5

Consider a command definition

Warning 1 in test.tex line 2: Command terminated with space.
\newcommand{\ket}[1]{\left \lvert #1 \right \rangle}  
                                 ^

This command is supposed to be used in math mode. However, the chktex linter complains that there's a "command terminated with space", presumably for the reasons described in this other post. Is there a recommended way to write the intended command, without getting a chktex warning?

8
  • 1
    You should be using the amsmath package.PS: If you wrap the definition in \ensuremath{...} you can use it in both math and text without a problem. Jul 3, 2020 at 18:54
  • 1
    It's a silly warning by chktex. Ignore it.
    – egreg
    Jul 3, 2020 at 19:51
  • 3
    if it takes a lot of effort to avoid incorrect warnings from a syntax checker, you have to ask if just not running the checker is the most effective solution. running latex is a more reliable check. Jul 3, 2020 at 20:28
  • 1
    @DavidCarlisle Isn't that a big like telling someone to ignore C compiler warnings because running the program is the most reliable check?
    – DanielSank
    Jul 4, 2020 at 21:43
  • 1
    No not at all, it is like telling someone to take heed of C compiler warnings, and not so much dubious advice from some third party claimed check program. latex does not give a warning here and the input is correct, chktex is not latex and does not parse the text in the same way. It may make some good suggestions but here it is simply wrong. I think it would be wrong to distort correct input just to avoid incorrect warnings from a lint program. The fix is to get the checker fixed not to change your document. Jul 4, 2020 at 22:58

2 Answers 2

8

As the maintainer of ChkTeX, this is one of the checks that I don't find personally useful. ChkTeX was originally written (I believe) for new users, and indeed this could be helpful for them because they may not know why \LaTeX and similar cause "missing spaces." I personally like linters a lot when they catch problems that you have. ChkTeX in particular needs some tweaking to be useful for an individual. So, if you don't think that this (or any other) check is useful then you should turn it off, e.g. by adding the following to your .chktexrc file:

CmdLine { --nowarn 1 }

That said, this warning does not fire in math mode, so putting it inside an \ensuremath is a perfectly reasonable solution, and using in the document would not cause a warning (since it would be in math mode).

You might also consider using the -H 0 command line arguments (e.g. in the CmdLine section) which turns off warnings in the preamble (i.e. before \begin{document} since there is often warning prone code there that isn't actually a problem (such as this).

In my opinion, adding it to Silent is the wrong solution since in practice it should always be used in math mode where it won't cause a warning.

3
  • 3
    The manual nowhere mentions “preamble”. Probably a linguistic problem, but “in front of \begin{document}” is really unclear; how's anybody supposed to know that -H 0 is the right key? How's anybody supposed to know that `Silent { <command> } adds to the list? How's anybody supposed to know that such warnings are not issued in math mode? Nothing about this is mentioned in the manual.
    – egreg
    Jul 5, 2020 at 19:44
  • 1
    You're absolutely right, the manual needs some work. In fact, I tried reading the manual to find the answer and had a hard time–and I knew it was there somewhere. I guess I need to spend some time updating it. Jul 6, 2020 at 4:20
  • 1
    The suggestion to use -H0 sounded good, but unfortunately I write my LaTeX documents modularly, i.e. I split sections into separate files and use \input (actually I use \subimportlevel from the modular package), so most of my tex files don't have a \begin{document} line.
    – DanielSank
    Jul 7, 2020 at 20:34
7

There is a simple way, namely

\newcommand{\ket}[1]{\left \lvert#1 \right \rangle}

This code is completely equivalent to yours. On the other hand, you'll get the same warning if you have

\lvert x\rvert

in your document. Write the maintainers and ask them to add the commands.

In the meantime, you can create a chktexrc.local file that you can save somewhere containing

Silent
{
    \rm \em \bf \it \sl \sf \sc \tt \selectfont
    \rmfamily \sffamily \ttfamily \mdseries \bfseries
    \slshape \scshape \relax
    \vskip \pagebreak \nopagebreak

    \textrm \textem \textbf \textit \textsl \textsf \textsc \texttt

    \clearpage \ddots \dotfill \flushbottom \fussy \indent \linebreak
    \onecolumn \pagebreak \pushtabs \poptabs \scriptsize \sloppy
    \twocolumn \vdots
    \today \kill \newline \thicklines \thinlines

    \columnsep \space \item \tiny \footnotesize \small \normalsize
    \normal \large \Large \LARGE \huge \Huge \printindex

    \newpage \listoffigures \listoftables \tableofcontents
    \maketitle \makeindex

    \hline \hrule \vrule

    \centering

    \bigskip \medskip \smallskip

    \noindent \expandafter


    \noindent \expandafter

    \makeatletter \makeatother

    \columnseprule

    \textwidth \textheight \hsize \vsize

    \if \fi \else

    \csname \endcsname

    \z@ \p@ \@warning \typeout

    \dots \ldots \input \endinput \nextline \leavevmode \cdots
    \appendix \listfiles \and \quad
    \hskip \vfill \vfil \hfill \hfil \topmargin \oddsidemargin
    \frenchspacing \nonfrenchspacing
    \begingroup \endgroup \par

    \vrefwarning \upshape \headheight \headsep \hoffset \voffset
    \cdot \qquad
    \left \right
    \qedhere

    \xspace

    \addlinespace \cr \fill \frontmatter
    \toprule \midrule \bottomrule

    \lvert \rvert

}[
# Here you can put regular expressions to match Silent macros.  It was
# designed for  the case where you  have many custom macros  sharing a
# common prefix, but can of course be used for other things.

# Support ConTeXt to at least some extent
\\start.* \\stop.*

]

and call chktexrc with

chktex -l path/to/chktexrc.local mydocument.tex

with the suitable replacement to path/to, of course.

Unfortunately there seems to be no way to add to the ignored keywords at runtime other than copying all of them and increment the list.

How did I find the original list? Find on your system the file chktexrc, open it and copy the relevant part.

Comment

It's a silly warning by chktex. It's silly because a space is the correct way to terminate a command. It should be the other way around: only some commands should not be terminated simply by a space, for instance \TeX or \LaTeX.

So there should be a NoSilent list, rather than a long list for which termination with a space is usually the best choice.

4
  • I disagree with the idea of a NoSilent list because then the user would have to remember to add ever command they make that should not have a space. This way, they will be reminded when a new command causes a warning. Though, again, I don't find this warning to be that useful personally. Jul 5, 2020 at 17:57
  • Using Silent { \lvert \rvert } should be enough to add them. If you want to override what's there, then you need to use Silent = { \lvert \rvert } Jul 5, 2020 at 18:09
  • 2
    @IvanAndrus I disagree with the disagreement. However, as I mentioned in my comment to your answer, nothing you write is specified in the manual. I mean: I appreciate your efforts, but the manual is not clear.
    – egreg
    Jul 5, 2020 at 19:46
  • Many thanks, I've been seeking for that option for long. Also think that it should be the default behaviour and vote for a NoSilent option.
    – gildux
    Feb 12 at 19:55

You must log in to answer this question.

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