为什么 latex 没有告诉我哪个命令未定义?

为什么 latex 没有告诉我哪个命令未定义?

从一开始,我就花了很多时间才得出以下这个最小的例子:

\PassOptionsToPackage{main=brazil,english,spanish,french}{babel}

\documentclass[
10pt,
a5paper,
]{memoir}

\makeatletter
    \@ifpackagewith{babel}{english}{\message{English YES!^^J}}{\message{English NO!^^J}}
    \@ifpackagewith{babel}{brazil}{\message{Brazil YES!^^J}}{\message{Brazil NO!^^J}}
    \@ifpackagewith{babel}{turkish}{\message{Turkish YES!^^J}}{\message{Turkish NO!^^J}}

    \@ifpackagewith{babel}{brazil}{\addto\captionsbrazil{\renewcommand{\lstlistingname}{Código}}}{}
    \@ifpackagewith{babel}{english}{\addto\captionsenglish{\renewcommand{\lstlistingname}{Code}}}{}
\makeatother

\begin{document}
    \chapter{Introduction}
    Thing.
\end{document}

使用以下命令运行该示例:

'latexmk.exe' -cd -f -pdf -interaction=nonstopmode -synctex=1 '--output-directory=cache' -latexoption=-file-line-error -latexoption=-halt-on-error -latexoption=--max-print-line=10000 test1.tex

$ tex --version
MiKTeX-TeX 2.9.6300 (3.14159265) (MiKTeX 2.9.6400)]

...
Package tabularx [2008/07/23] emulated by memoir.
Package titleref [2008/07/23] emulated by memoir.
Package titling [2008/07/23] emulated by memoir.
Package tocbibind [2008/07/23] emulated by memoir.
Package tocloft [2008/07/23] emulated by memoir.
Package tocvsec2 [2008/07/23] emulated by memoir.
Package verbatim [2008/07/23] emulated by memoir.
Package verse [2008/07/23] emulated by memoir.
 (D:\User\Documents\latex\texmfs\install\tex\latex\memoir\mempatch.sty
Package: mempatch 2009/07/24 v6.0f Patches for memoir class v1.6180339
)) English YES!
Brazil NO!
Turkish NO!
test1.tex:17: Undefined control sequence 
Here is how much of TeX's memory you used:
 3506 strings out of 493314
 47031 string characters out of 3134142
 129114 words of memory out of 3000000
 7118 multiletter control sequences out of 15000+200000
 4238 words of font info for 16 fonts, out of 3000000 for 9000
 1141 hyphenation exceptions out of 8191
 24i,1n,22p,129b,62s stack positions out of 5000i,500n,10000p,200000b,50000s

test1.tex:17:  ==> Fatal error occurred, no output PDF file produced!

Latex 不会在日志中告诉我哪个命令未定义。它只会说Undefined control sequence,这毫无用处。

我怎样才能让 Latex 准确地告诉我未找到定义哪个命令?

经过多次尝试,我才发现它ifpackagewith并不检查包是否已加载。它只检查包名称队列中是否有某个选项。

然后,一个固定的版本是:

\makeatletter
    \@ifpackageloaded{babel}{\@ifpackagewith{babel}{english}{\message{English YES!^^J}}{\message{English NO!^^J}}}{}
    \@ifpackageloaded{babel}{\@ifpackagewith{babel}{brazil}{\message{Brazil YES!^^J}}{\message{Brazil NO!^^J}}}{}
    \@ifpackageloaded{babel}{\@ifpackagewith{babel}{turkish}{\message{Turkish YES!^^J}}{\message{Turkish NO!^^J}}}{}

    \@ifpackageloaded{babel}{\@ifpackagewith{babel}{brazil}{\addto\captionsbrazil{\renewcommand{\lstlistingname}{Código}}}{}}{}
    \@ifpackageloaded{babel}{\@ifpackagewith{babel}{english}{\addto\captionsenglish{\renewcommand{\lstlistingname}{Code}}}{}}{}
\makeatother

babel或者只需用加载包即可\RequirePackage{babel}


现在,忘记修复,即加载babel或使用\@ifpackageloaded。为什么 latex 从一开始就不告诉我哪个命令未定义???

如果我删除-latexoption=-halt-on-error,那么 latex 会告诉我命令\lstlistingname未定义,但这是错误的命令。未定义的命令来自\add\to\captionsbabel加载的包。

参见此处的 Latex 输出:

'latexmk.exe' -cd -f -pdf -interaction=nonstopmode -synctex=1 '--output-directory=cache' -latexoption=-file-line-error -latexoption=--max-print-line=10000 test1.tex

x\memoir\mempatch.sty)) English YES!
Brazil NO!
Turkish NO!
test1.tex:15: Undefined control sequence
test1.tex:15: Undefined control sequence

test1.tex:15: LaTeX Error: \lstlistingname undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help

\lstlistingname如果我从输入程序中删除如下内容:

\makeatletter
    \@ifpackagewith{babel}{english}{\message{English YES!^^J}}{\message{English NO!^^J}}
    \@ifpackagewith{babel}{brazil}{\message{Brazil YES!^^J}}{\message{Brazil NO!^^J}}
    \@ifpackagewith{babel}{turkish}{\message{Turkish YES!^^J}}{\message{Turkish NO!^^J}}

    \@ifpackagewith{babel}{brazil}{\addto\captionsbrazil{}}{}
    \@ifpackagewith{babel}{english}{\addto\captionsenglish{}}{}
\makeatother

然后再次运行 latex,没有-interaction=nonstopmodeand -latexoption=-halt-on-error,那么 latex 根本就不会告诉我哪个命令未定义!

'latexmk.exe'-cd-f-pdf'--output-directory=cache'-latexoption=-file-line-error-latexoption=--max-print-line=10000 test1.tex

Latexmk: This is Latexmk, John Collins, 19 Jan. 2017, version: 4.52c.
Latexmk: Changing directory to './'
Latexmk: making output directory 'cache'
Latexmk: applying rule 'pdflatex'...
Rule 'pdflatex': Rules & subrules not known to be previously run:
   pdflatex
Rule 'pdflatex': The following rules & subrules became out-of-date:
      'pdflatex'
------------
Run number 1 of rule 'pdflatex'
------------
------------
Running 'pdflatex  -file-line-error --max-print-line=10000 -recorder -output-directory="cache"  "test1.tex"'
------------
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6400)
entering extended mode
(test1.tex
LaTeX2e <2017-04-15>
Babel <3.12> and hyphenation patterns for 76 language(s) loaded.
(D:\User\Documents\latex\texmfs\install\tex\latex\memoir\memoir.cls
Document Class: memoir 2016/05/16 v3.7f configurable book, report, article document class
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\ifpdf.sty) (D:\User\Documents\latex\texmfs\install\tex\latex\ifetex\ifetex.sty (D:\User\Documents\latex\texmfs\install\tex\plain\ifetex\ifetex.tex)) (D:\User\Documents\latex\texmfs\install\tex\generic\ifxetex\ifxetex.sty) (D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\ifluatex.sty) (D:\User\Documents\latex\texmfs\install\tex\latex\memoir\mem10.clo) (D:\User\Documents\latex\texmfs\install\tex\latex\memoir\mempatch.sty)) English YES!
Brazil NO!
Turkish NO!
test1.tex:15: Undefined control sequence
test1.tex:15: Undefined control sequence
(D:\Test\cache\test1.aux) [1{D:/User/Documents/latex/texmfs/data/pdftex/config/pdftex.map}] (D:\Test\cache\test1.aux) )
(see the transcript file for additional information)<D:/User/Documents/latex/texmfs/install/fonts/type1/public/amsfonts/cm/cmbx12.pfb><D:/User/Documents/latex/texmfs/install/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on D:\Test\cache\test1.pdf (1 page, 21716 bytes).
Transcript written on D:\Test\cache\test1.log.
=== TeX engine is 'pdfTeX'
Latexmk: Log file says output to 'cache/test1.pdf'
Latexmk: applying rule 'pdflatex'...
Rule 'pdflatex': File changes, etc:
   Changed files, or newly in use since previous run(s):
      'cache/test1.aux'
------------
Run number 2 of rule 'pdflatex'
------------
------------
Running 'pdflatex  -file-line-error --max-print-line=10000 -recorder -output-directory="cache"  "test1.tex"'
------------
This is pdfTeX, Version 3.14159265-2.6-1.40.18 (MiKTeX 2.9.6400)
entering extended mode
(test1.tex
LaTeX2e <2017-04-15>
Babel <3.12> and hyphenation patterns for 76 language(s) loaded.
(D:\User\Documents\latex\texmfs\install\tex\latex\memoir\memoir.cls
Document Class: memoir 2016/05/16 v3.7f configurable book, report, article document class
(D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\ifpdf.sty) (D:\User\Documents\latex\texmfs\install\tex\latex\ifetex\ifetex.sty (D:\User\Documents\latex\texmfs\install\tex\plain\ifetex\ifetex.tex)) (D:\User\Documents\latex\texmfs\install\tex\generic\ifxetex\ifxetex.sty) (D:\User\Documents\latex\texmfs\install\tex\generic\oberdiek\ifluatex.sty) (D:\User\Documents\latex\texmfs\install\tex\latex\memoir\mem10.clo) (D:\User\Documents\latex\texmfs\install\tex\latex\memoir\mempatch.sty)) English YES!
Brazil NO!
Turkish NO!
test1.tex:15: Undefined control sequence
test1.tex:15: Undefined control sequence
(D:\Test\cache\test1.aux) [1{D:/User/Documents/latex/texmfs/data/pdftex/config/pdftex.map}] (D:\Test\cache\test1.aux) )
(see the transcript file for additional information)<D:/User/Documents/latex/texmfs/install/fonts/type1/public/amsfonts/cm/cmbx12.pfb><D:/User/Documents/latex/texmfs/install/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on D:\Test\cache\test1.pdf (1 page, 21716 bytes).
Transcript written on D:\Test\cache\test1.log.
=== TeX engine is 'pdfTeX'
Latexmk: Log file says output to 'cache/test1.pdf'
Latexmk: Errors, in force_mode: so I tried finishing targets
Collected error summary (may duplicate other messages):
  pdflatex: Command for 'pdflatex' gave return code 1
      Refer to 'cache/test1.log' for details
Latexmk: Undoing directory change

为什么 latex 不告诉哪个命令未定义?这会伤害他吗?他是否担心信息太多而我无法自己处理?

是否有一些修复方法可以应用于 Latex,以便当出现错误时它会开始告诉我哪些命令未定义?

参考:

  1. http://tex.stackexchange.com/questions/228936/setting-entries-of-list-of-listings-in-latex-package-listings
  2. 测试包(或包选项)是否已加载

相关内容