我有这个文件:
\documentclass{report}
\usepackage[english, spanish]{babel}
\begin{document}
\selectlanguage{english}
\section*{Hello, world!}
This is a document written in English.
\selectlanguage{spanish}
\section*{¡Hola, mundo!}
Este es un documento escrito en español.
\end{document}
我执行以下命令来生成西班牙语的 PDF:
pdflatex -jobname=manual-es "\AtBeginDocument{\selectlanguage{spanish}}" manual.tex
但我收到以下错误:
This is pdfTeX, Version 3.141592653-2.6-1.40.24 (MiKTeX 22.10) (preloaded format=pdflatex.fmt)
restricted \write18 enabled.
entering extended mode
LaTeX2e <2022-06-01> patch level 5
L3 programming layer <2022-09-28>
! LaTeX Error: Missing \begin{document}.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
<*> \AtBeginDocument{\selectlanguage{spanish}} m
anual.tex
? return
OK, entering \nonstopmode...
Missing character: There is no m in font nullfont!
Missing character: There is no a in font nullfont!
Missing character: There is no n in font nullfont!
Missing character: There is no u in font nullfont!
Missing character: There is no a in font nullfont!
Missing character: There is no l in font nullfont!
Missing character: There is no . in font nullfont!
Missing character: There is no t in font nullfont!
Missing character: There is no e in font nullfont!
Missing character: There is no x in font nullfont!
! Emergency stop.
<*> ...cument{\selectlanguage{spanish}} manual.tex
! ==> Fatal error occurred, no output PDF file produced!
Transcript written on manual-es.log.
pdflatex: major issue: So far, you have not checked for MiKTeX updates.
有人知道为什么会发生这种情况吗?
先感谢您。
答案1
由于第一个参数是源而不是文件名,因此其后的所有内容也被视为源。
你需要manual.tex
以一种扩展到其内容的方式包含
pdflatex -jobname=manual-es "\AtBeginDocument{\selectlanguage{spanish}}\input{manual.tex}"
但是,它\selectlanguage
仅仅告诉您babel
当前正在排版哪种语言,对于有选择地挑选出一些文本进行排版没有帮助。
为了有选择地排版,我们可以遵循这个答案并使用comment
。
\documentclass{report}
\usepackage[english, spanish]{babel}
\usepackage{comment}
\specialcomment{english}{\selectlanguage{english}}{}
\specialcomment{spanish}{\selectlanguage{spanish}}{}
\begin{document}
\begin{english}
\section*{Hello, world!}
This is a document written in English.
\end{english}
\begin{spanish}
\section*{¡Hola, mundo!}
Este es un documento escrito en español.
\end{spanish}
\end{document}
现在,english
环境中的所有内容都可以用\excludecomment{english}
和 来省略spanish
。这可以通过以下方式实现
pdflatex -jobname=manual-es "\AtBeginDocument{\excludecomment{english}}\input{manual.tex}"
或者我们可以对编译进行条件控制\jobname
,这样在编译时就不需要传递任何额外的代码,只需使用以下任一方法即可:
这个答案
\input \jobname
在同一个目录中添加wheremanual-es.tex
contains\excludecomment{english}
和manual-en.tex
contains\excludecomment{spanish}
。TeX 常见问题解答,将
\StripPrefix
和\jobis
定义复制到manual.tex
,并使用\if\jobis{manual-es} \excludecomment{english} \fi \if\jobis{manual-en} \excludecomment{spanish} \fi
然后我们只需要编译为
pdflatex -jobname=manual-es manual
可以通过将for all languages\excludecomment{<other language>}
放入其中来替换限制性s ,然后将 s 替换为in或 inside,具体取决于您使用的内容。这将使编译“正常”显示所有语言环境,而不是全部。\excludecomment{<language>}
manual.tex
\excludecomment{<other language>}
\includecomment{<this language>}
manual-en.tex
\ifjobis{<language>}
manual.tex