5

I have this document:

\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}

And I execute this command to generate a PDF for Spanish:

pdflatex -jobname=manual-es "\AtBeginDocument{\selectlanguage{spanish}}" manual.tex

but I get the following error:

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.

Does anyone know why this might happen?

Thank you in advance.

  • With this command no longer gives error but not generate only one language

    pdflatex -jobname=.\out\manual-english "\AtBeginDocument{\selectlanguage{english}}\input{manual}"

    – Alex Segura Feb 17 '23 at 15:54

1 Answers1

5

As the first argument is source rather than a filename, everything following it is also being treated as source.

You instead need to include manual.tex in a way that expands to its contents

pdflatex -jobname=manual-es "\AtBeginDocument{\selectlanguage{spanish}}\input{manual.tex}"

However, \selectlanguage just tells babel which language is currently being typeset, it doesn't help with selectively picking out some text to typeset.

In order to selectively typeset, we can follow this answer and use 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}

Now everything within the english environments can be omitted with \excludecomment{english} and similar for spanish. This can be achieved with

pdflatex -jobname=manual-es "\AtBeginDocument{\excludecomment{english}}\input{manual.tex}"

Or we can condition the compilation on \jobname, so no additional code needs to be passed at compile time, with either of:

  1. This answer adding \input \jobname where in the same directory manual-es.tex contains \excludecomment{english} and manual-en.tex contains \excludecomment{spanish}.

  2. This TeX FAQ, copying the \StripPrefix and \jobis definitions into manual.tex, and using

    \if\jobis{manual-es}
      \excludecomment{english}
    \fi
    \if\jobis{manual-en}
      \excludecomment{spanish}
    \fi
    

Then we only need to compile as

pdflatex -jobname=manual-es manual

The restrictive \excludecomment{<other language>}s can be replaced by putting \excludecomment{<language>} for all languages in manual.tex and then the \excludecomment{<other language>}s should be replaced by \includecomment{<this language>} in either manual-en.tex etc or inside the \ifjobis{<language>} depending what you use. This would make compiling manual.tex "normally" show none of the language environments rather than all.

Dai Bowen
  • 6,117