<< | Page list | >>
Script that provides Grammar Checking support within LyX.
1. Goal
Detect common errors in LyX documents, such as missing spaces after mathblocks, incorrect use of interword spacing and grammatical errors, from within the LyX GUI. This tool can also be used on raw TeX without involving LyX.
2. Solution
Run the script lyx-gc
to start Lyx with grammar check support.
LyX already has support for checking for "TeX Errors" such as incorrect use of interword spacing from within the GUI. It does this by calling a program called chktex
. This script includes a more powerful Perl-based version of chktex
instead that can detect LyX specific errors (such as too little space after math blocks) and common grammatical errors, and alters the path so that LyX uses this more powerful chktex
.
It does not generate errors if it is too stupid to understand your grammar. Instead, it will report an error if your text matches one of ~400 rules describing common errors. It is relatively easy for a technically inclined person to disable or customize each rule.
3. Compatibility
This still works with LyX 2.1.4, LanguageTool 3.4, chktex 1.7.5 as of 1.06.2016
If you did not have chktex installed before, you need to reconfigure LyX before the menu item shows up, or replace
\chktex_command ""
with
\chktex_command "chktex -n1 -n3 -n6 -n9 -n22 -n25 -n30 -n38"
in lyxrc.defaults and restart lyx.
Tested with:
- Perl v5.6.1, v5.8.3, v5.8.4, v5.8.6;
- LyX v1.2.1, v1.3.6, v1.3.7, v1.4.0pre3, lyx1.4.4, 2.1.4;
- Redhat 7.2, Fedora Core4&6, Ubuntu 5.10 6.06, Mac OS X, Debian Sarge;
- ChkTeX v1.6.3 http://baruch.ev-en.org/proj/chktex/, ChkTeX 1.7.5
- JLanguageTool v0.7.1, v0.7.2, v0.8.1, v0.8.2, v0.8.5, v0.8.8, v0.9., 3.4
->It does NOT work with v0.8.4, To use v0.8.5 of JLanguageTool you will need v0.3g of LyX-gc or later.
JLanguageTool is under rapid development at the moment. Supporting v0.8.1
required a fix (a single-*character* fix, but a fix none-the-less). If
LyX-GC does not seem to work with the most recent version of Language tool email me (my gmail username is gmatht, or use the mailto link below).
4. Files
It is simplest to just download the latest version from git; lyx-gc-0.3f.zip and earlier do not work with the current version of LanguageTool. LyX-gc 0.3h is tested against LanguageTool 0.8.8.
You also need Perl, if it is not already installed, and various standard UNIX utilities like bash and nice.
For full functionality you will also want the following software
You are also able to use lacheck with the LyX-GC. However lacheck
implements largely the same functionality as ChkTeX, and ChkTeX works better with LyX.
Grammar Checking
5. Installation
No installation is required for LyX-GC, just unzip it. Install chktex
and JLanguageTool
as described according to their docs if you wish to use these extensions.
6. Configuration
No configuration should be required for basic use under Linux (e.g. no setup should be required to detect errors such as "An person"). For Mac OS X, you may have to change the LYX variable in the chktex
wrapper in the lyx-gc-0.3d
directory. I have not tested this script under Windows, but it should just work if you are using Cygwin.
To enable use of JLanguageTool
, you will have to change chktex.pl.JLanguageTool.pl
so that JAVA equals the full name and path of the official Sun Java 1.4 binary, and LANGUAGETOOL_PATH equals the path of JLanguageTool
. To check that it is set up correctly enter "is were" into a new file and activate Check Tex twice (In LyX 1.4 Check Tex is in the Tools menu, prior to 1.4 it was in the Edit menu). An error notification should appear after the second activation.
The LyX-GC script assumes that chktex
is installed in /usr/bin/chktex
. If this is not the case, replace /usr/bin/chktex
with the actual path of chktex in the file chktex.pl
.
At present to customize LyX-GC you have to edit chktex.pl
. To customize or remove an in[tf]ernally generated error, (error #666) you will have to find ErrorTypes=(
in chktex.pl
and edit the ErrorTypes array. To disable a chktex rule (error # < 666) find where we call /usr/bin/chktex
and add -n99 to the command line, where 99 is the number of the rule to disable. To disable a JLanguageTool Error
(e.g. WORD_REPEAT_RULE) find ignore_rules=(
in add the error name to it. You may also be able to customize JLanguageTool
error rule by editing JLanguageTool
's .xml file for your language.
By default, the short-cut for buffer-chktex
is M-e h
(e.g Alt-e
then h
). You may wish to change this to F8
by creating a new bind-file, say $HOME/.lyx/bind/myShortcuts.bind
and include one of the standard files, e.g.:
# include one of the basic flavours (cua or emacs)
\bind_file "cua"
# add your own bindings (overwriting the included ones)
\bind "F8" "buffer-chktex"
Then go to the preferences dialog and choose this file to be your bind-file. See KeyboardShortcuts for more information.
7. Usage
Run the lyx-gc
script in place of LyX. Now, when you click on Check Tex element in the Tools menu (or Edit menu if using LyX version less than 1.4), LyX-GC will check your document for grammatical errors. If no grammar errors are found, the Check Tex option will check your document for tex errors as normal. If the Check Tex option is grayed out, click on the Reconfigure option and restart LyX-GC.
If you are not using LyX you can run lyx-gc-0.4/chktex MyTeXFile.tex
to check the grammar of a TeX file.
8. Example
For a quick demonstration of LyX-GrammarChecker, follow the following steps:
- Run
lyx-gc
- Open the included
SampleGrammarErrors.lyx
file
- Activate the Check Tex option
- Activate the Check Tex option again (you should now get at least one warning per paragraph if
JLanguageTool
is correctly installed and configured).
9. See Also
10. False Positives
If you have a large document you may end up with many false positives (sorry). Say you have run LyX-GC on FooBar.tex, and have found that all of the errors are false positives or maybe just minor stuff that you don't care about. LyX-GC (v0.4 or higher) will have logged the false positives in ~/.lyx-gc/FooBar.tex.positives. You can tell LyX-GC that all of the "errors" it has found are false positives by moving this file to ~/.lyx-gc/FooBar.tex.falsepostive. Then LyX-GC will only report new errors.
11. History
- v0.4: Now detects inappropriately capitalized words using aspell (this will work better if aspell is installed).
* Support for marking errors as false-positives.
- v0.3e: No longer needs to be installed by the same user that runs it.
- v0.3d: Now detects which UI LyX uses, and enables qt and xforms specific hacks to allow use of newlines and colons in ChkTeX errors.
- v0.3c: Now reparses ChkTeX errors, allowing context information to be included, and also fixing the bug with filenames containing ":".
* Includes errors from lacheck
, if lacheck
is available.
* Flattened directory structure, removing lyx-gc.dir
.
* Better chktex compatibility, supporting all -v
output formats.
- v0.3: added wrapper for ease of use, and replaced LaTeX bindings with chktex ones.
12. Bugs
- To get up to date JLanguageTool errors, You have to activate Check Tex twice. This is intentional as JLanguageTool can take several seconds to complete and I don't want LyX to freeze for that amount of time.
- If your file system does not support
flock
, there is no guarantee that the errors above will be up-to-date after the second activation, unless you manually wait for JLanguageTool to complete before you activate Check Tex for the second time.
- This script does not sort warnings according to line number. This does not matter with LyX versions prior to 1.4, but v1.4 no longer sorts the warnings for us. Actually, there is probably no need to sort the errors by line number.
- v0.3d uses UTF-8 specific characters. If $LANG does not end in ".UTF-8" then these characters will display as funny looking characters.
- I consider the error dialog in 1.4.{0,1,2} to be unusable due to http://bugzilla.lyx.org/show_bug.cgi?id=2179
This error is fixed in 1.4.3, and is in some sense better than than in 1.3.x, as it highlights the line of text in which the error occurs, rather than just putting an error box in the rough vicinity of the error. It would be better if it told you exactly where the error was in the GUI though.
13. To Do
- Translate from Perl into Python and integrate with LyX.
- Fix the known bug with JLanguageTool, possibly using IPC or alternatively the API for calling Java libraries from non-Java applications, if such a beast exists. LanguageTool now has a TCP based API. We could use this.
- Add support for non-technical users to give reasons why a piece of text should or should not match a particular rule. Then add general purpose code that will allow any rule (including rules not yet written) to be customized according to the user specified information.
- Export JLanguageTool's German language checking capabilities so we can check the grammar of German documents as well.
- Fix it so that it sorts errors by line number. (only useful with LyX > 1.3)
- Add support in LyX for ":" and "\n" in ChkTeX errors (done for lyx-qt). Heck, rich text (e.g. bold, mono-space etc.) would be nice too.
- Test it under MS-Windows.
- Allow the user to define silently enforced grammar rules E.g. silently replacing
McCabe...
with
M\textsuperscript{c}Cabe
So that the user does not have to use ERT every time they type in McCabe and want the "c" to be raised properly.
14. Contributors
(Also included are Daniel Naber's det_a, and det_an word lists)
15. Copyright
LyX-GC is (C) 2005-7 by John C. McCabe-Dansted, you may distribute it under the terms of the GPL version 2 or later; or email me if these terms are not suitable.
Grammar Checking