[Tex/LaTex] “Runaway argument?” just in the \input file

conditionalsetoolboxverbatim

Update: Upon @egreg suggestion, the case shown below is the core of the problem and has nothing to do with include/input statements. I have a verbatim within toggle statements, which fails. A minimal code for that is

\documentclass[openany]{book}
\usepackage{etoolbox}

\newtoggle{tog1}
\toggletrue{tog1}

\begin{document}
    \iftoggle{tog1} {
         \begin{verbatim}
             Hello World!
         \end{verbatim}
    } {
        \textit{Hello World}
    }
\end{document}


TL;DR I have toggle cases within which I have a verbatim and this leads to

"Runaway argument" Content of verbatim ! File ended while scanning use of \@xverbatim.


I have a peculiar case. I have a latex project with the below directory structure

[project_home]
    - main.tex
    - [tex]
        - [folder_1]
            - chapter_1.tex
            - chapter_1_sec_1.tex
        - [folder_2]
            - chapter_2.tex
            - chapter_2_sec_1.tex
    - [img]
        <image files>

The main.tex file contains include statements to include files from the tex folders (they pertain to the individual chapters).

main.tex

\documentclass{book}
\newtoggle{tog1}
\toggletrue{tog1}
..
\begin{document}
    \include{tex/folder_1/chapter_1}
    \include{tex/folder_2/chapter_2}
    ..
\end{document}

Within each chapter, I have many sections. I am including these individual section files within the chapter.tex and some of these section files have toggle statements (etoolbox).

chapter_1.tex

\chapter{chapter 1}
    \section{section 1}
         \input{chapter_1_sec_1.tex}
         ..
    \section{section 2}
         ..

chapter_1_sec_1.tex

\iftoggle{tog1} {
    \begin{verbatim}
        % some HTML code
    \end{verbatim}
} {
    % The else part
    % No verbatim here, just plain tex text
}

Now, with the included \verbatim or \lstlisting within these toggle cases, the build fails with the above mentioned error.

Note 1: The build will not fail if I remove the toggle cases. The verbatim/lstlisting works just fine when I remove the toggle cases and just include the content within the file.

Note 2: It will work if I remove the verbatim/lstlisting sections and have the toggle cases.

I tried including the contents of the chapter_1_sec_1.tex directly in the chapter_1.tex file without the toggle cases. So, there seems to be no problem with nested include/input files.

The ONLY case when it fails is if I have verbatim within toggles. I am relatively new to latex and I am not sure if this is an obvious mistake. Could someone point out a correct way/workaround to this problem.
Also, why does this error occur?

Best Answer

verbatim content is currently being supplied as an argument to \iftoggle{<toggle>}{<true>}{<false>} - either in the <true> or the <false> clause. You can't have verbatim content as the argument of a macro as the argument being read fixes the category codes, which verbatim wants to change.

One suggestion is to use traditional TeX \ifs:

\newif\iftogA
\togAtrue% Set toggle togA to true

and then inside chapter_1_sec_1.tex

\iftogA
    \begin{verbatim}
        % some HTML code
    \end{verbatim}
\else
    % The else part
    % No verbatim here, just plain TeX text
\fi

Here's a minimal example highlighting the use:

enter image description here

\documentclass{article}

\newif\iftogA

\begin{document}

\togAtrue% Set toggle togA to true
\iftogA
\begin{verbatim}
Some verbatim text.
\end{verbatim}
\else
Some regular text.
\fi

\togAfalse% Set toggle togA to false
\iftogA
\begin{verbatim}
Some verbatim text.
\end{verbatim}
\else
Some regular text.
\fi

\end{document}

An issue that is not covered in the question is that of your verbatim environment indentation. Note that leading spaces are set as-is under verbatim (and listings), so it's typically advised to avoid that (as I did above).

Related Question