17

In Emacs, in order to skip spelling for the arguments of a user macro, one appends an entry to the ispell-tex-skip-alists variable.

For example, to skip parsing the argument of \mycommad, one adds an entry like

("\\\\mycommad"    ispell-tex-arg-end) 

The complete command, perhaps in the init file, should be

(setq ispell-tex-skip-alists
   (list
    (append
     (car ispell-tex-skip-alists) ;tell ispell to ignore content of this:
     '(("\\\\mycommad"       ispell-tex-arg-end)))
(cadr ispell-tex-skip-alists)))

So the text inside \mycommad{blah blah} will be skipped.

But what if the command takes two arguments and I want to skip only the spelling of the second? That is

\mycommad{spell this}{skip this}

Documentation for using ispell-tex-skip-alists is very limited. Particularly, ispell-tex-arg-end sometime takes a numeric argument, what is it?

5
  • 1
    ispell-tex-arg-end takes as argument the number of the first mandatory argument to spell. E.g., ("\\\\addcontentsline" ispell-tex-arg-end 2) means that the first mandatory argument will be skipped, the second (and following) will be spelled.
    – giordano
    Jun 2, 2013 at 12:40
  • +1 @giordano: I was looking for spelling the first and skipping the second. So ispell-tex-arg-end 1 should be skip none and spell everything starting from the first. I have seen also ispell-tex-arg-end 0, what is it for? And, please, where can I find documentation for this topic? The .el file is very limited.
    – antonio
    Jun 2, 2013 at 16:41
  • I've just read definition of ispell-tex-arg-end, in ispell.tex. You can find it with C-h f ispell-tex-arg-end RET. I have to correct: the argument of ispell-tex-arg-end is the first argument to spell minus one. The first argument of \addcontentsline to be spelled will be the third. I suppose that a 0 arg means "spell all arguments", though it looks strange to me. I don't know how to do what you want.
    – giordano
    Jun 2, 2013 at 17:22
  • In any case I have a partial solution, as I could swap the macro arguments. As for the zero, in ispell.el I found: ("\\\\makebox" ispell-tex-arg-end 0).
    – antonio
    Jun 2, 2013 at 17:42
  • maybe 0 means to skip optional arguments, but I'm not sure.
    – giordano
    Jun 2, 2013 at 17:46

1 Answer 1

10

Theory

This is a long answer to using ispell-tex-skip-alists and ispell-tex-arg-end, but you might want to go straight to the proposed solutions.

Upon examination of ispell.el Lisp code in Emacs, I found what follows.

The skip command template to append to your Emacs init file is:

(setq ispell-tex-skip-alists
      (list
       (append
    (car ispell-tex-skip-alists) 
    '(
      ("\\\\mycommand"       ispell-tex-arg-end)
      ("\\\\mycommandtwo"    ispell-tex-arg-end 2)
      ;; add as many lines like the previous two as you need before the "))"  
      ))
       (cadr ispell-tex-skip-alists)))

Note that I used an unusual formatting to make line insertion easy for non-Lisp users. Ordinary formatting would be:

(setq ispell-tex-skip-alists
      (list
       (append
    (car ispell-tex-skip-alists) 
    '(("\\\\mycommand"       ispell-tex-arg-end)
      ("\\\\mycommandtwo"      ispell-tex-arg-end 2)))
       (cadr ispell-tex-skip-alists)))

In the preceding code \\\\mycommand is any regular expression identifying the beginning of the text region to be skipped from spelling. If you want to tell Emacs to use a particular regular expression to mark the ending of the skip region, use a skip line like:

("\\\\mycommand"       "myregexp")

where "myregexp" is a placeholder for your regular expression.

Most often, our command is like: \mycommand{text to skip}; therefore the brace } is the marker for the skip-region ending and this suggests writing:

("\\\\mycommand"       "}")

However, in most cases, a LaTeX command might include optional LaTeX arguments in square brackets hosting command parameters that do not need any spelling, such as:

\mycommand[skip this][skip this]{text to skip} 

To skip the content in square brackets, instead of using "}" to denote the end of the skip region, you use:

("\\\\mycommand"   ispell-tex-arg-end)

Formally ispell-tex-arg-end is a function built into the spelling library, which causes skipping the spelling of any command arguments in square brackets.

ispell-tex-arg-end has also another important feature. Normally a second macro argument will not be skipped, such as in:

\mycommand[skip this][skip this]{text to skip}{I will be spell-checked} 

However, you can override this behaviour by giving an integer argument to ispell-tex-arg-end, i.e.:

("\\\\mycommand"   ispell-tex-arg-end n)

In this case, Emacs spelling will skip n braced-arguments of \mycommand. Omitting n is like passing 1. Also, if n is 0, then you will only skip the arguments in square brackets.

The latter feature answers to the second part of the question concerning the use of ispell-tex-skip-alists and ispell-tex-arg-end.

And now for the more relevant parts.

Solution 1

Assuming you are the owner of \mycommandand you can/want to slightly modify it, just redefine it so to swap the arguments. Therefore, it will work like this:

\mycommand{skip this}{spell this}

Append to your Emacs init file:

(setq ispell-tex-skip-alists
      (list
       (append
    (car ispell-tex-skip-alists) 
    '(("\\\\mycommand"   ispell-tex-arg-end 1)))
       (cadr ispell-tex-skip-alists)))

Number 1 after ispell-tex-arg-end is used for clarity, but you might omit it.

Solution 2

If redefining \mycommandis not an option for you, you can define:

\newcommand{\mycommandSpell}[2]{%
\mycommand{#2}{#1}}

Append to your Emacs init file:

(setq ispell-tex-skip-alists
      (list
       (append
    (car ispell-tex-skip-alists) 
    '(("\\\\mycommandSpell"   ispell-tex-arg-end 1)))
       (cadr ispell-tex-skip-alists)))

As for the previous solution, number 1 after ispell-tex-arg-end is just used for clarity.

For the curious

Why are there four slashes in "\\\\mycommand" regular expression string? First you have to double the slash in \mycommand to tell to the Lisp interpreter the slash is not there to signify an escape sequence (like the newline "\n"). You have to double the doubled-slash a second time because, in a similar fashion, the regexp engine interprets a single slash like a command suffix, much like in LaTeX. So when you write \\\\mycommand, you are telling \\mycommand to Lisp and, when Lisp passes \\mycommand to the regexp parser, it will look for the string "\mycommand" as is, instead of interpreting "\m" as a command.
Note that to search for a brace, "{", you simply use {, but use \\[ to search for a square bracket.

2
  • 1
    In the solutions, is the 2 really correct? I think it should be 1, or omitted. May 19, 2021 at 7:33
  • 1
    @TorstenBronger: It was a copy and paste typo, fixed, thank you,
    – antonio
    May 20, 2021 at 16:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .