Environment inside a command

1,392

Solution 1

from http://www.tex.ac.uk/cgi-bin/texfaq2html?label=verbwithin

The LaTeX verbatim commands work by changing category codes. Knuth says of this sort of thing “Some care is needed to get the timing right…”, since once the category code has been assigned to a character, it doesn’t change. So \verb and \begin{verbatim} have to assume that they are getting the first look at the parameter text; if they aren’t, TeX has already assigned category codes so that the verbatim command doesn’t have a chance. For example:

\verb+\error+

will work (typesetting ‘\error’), but

\newcommand{\unbrace}[1]{#1}
\unbrace{\verb+\error+}

will not (it will attempt to execute \error). Other errors one may encounter are ‘\verb ended by end of line’, or even the rather more helpful ‘\verb illegal in command argument’. The same sorts of thing happen with \begin{verbatim}\end{verbatim}:

\ifthenelse{\boolean{foo}}{%`
\begin{verbatim}
foobar
\end{verbatim}
}{%
\begin{verbatim}
barfoo
\end{verbatim}
}

provokes errors like ‘File ended while scanning use of \@xverbatim’, as \begin{verbatim} fails to see its matching \end{verbatim}.

This is why the LaTeX book insists that verbatim commands must not appear in the argument of any other command; they aren’t just fragile, they’re quite unusable in any “normal” command parameter, regardless of \protection. (The \verb command tries hard to detect if you’re misusing it; unfortunately, it can’t always do so, and the error message is therefore not reliable as an indication of problems.)

So the first question to ask yourself is: “is \verb actually necessary?”.

If `\texttt{your text}` produces the same result as `\verb+your text+`,

then there’s no need of \verb in the first place. If you’re using \verb to typeset a URL or email address or the like, then the \url command from the url package will help: it doesn’t suffer from all the problems of \verb, though it’s still not robust. If you’re putting \verb into the argument of a boxing command (such as \fbox), consider using the lrbox environment:

    `\newsavebox{\mybox}`
    ...
    `\begin{lrbox}{\mybox}`
      `\verb!VerbatimStuff!`
    `\end{lrbox}`
    `\fbox{\usebox{\mybox}}`

[And so on. More to be had in the web page at top]

Solution 2

I have big doubts about the \# 1 in the code, perhaps you want some space before and perhaps the 1 should be the value of some counter, anyhow, you can rescue the \QUE command in the following manner: (as is well explained in wasteofspace's answer, one can not put a verbatim inside what will serve as parameter to a command, so the trick here is that \QUE has no parameter)

\documentclass[a4paper,12pt,twoside]{article}
\usepackage[english]{babel}
\usepackage{amssymb}
\usepackage{amsmath}

% \newcommand{\QUE}[1]{%
% {\bf Question\# 1 }: #1
% }%end QUE

\newcommand{\QUE}{\textbf {Question\# 1}: \bgroup\let\next=}%

\raggedbottom

\begin{document}\thispagestyle{empty}

\section{Algorithms and data structures} 

\QUE{Given the following C function definition:
\begin{verbatim}
int foo(int a, int b){
  if (a == b)
    return a;
  else
    return (a>b) ? foo(a-b, b) : foo(a, b-a);
}
\end{verbatim}
What is the result of the computation of \texttt{foo(24, 16)}?
}

\smallskip\hrule\smallskip

\end{document}

verbatim "in" command

Share:
1,392

Related videos on Youtube

Flavio Sartoretto
Author by

Flavio Sartoretto

Associate Professor of Scientific Computing

Updated on October 24, 2020

Comments

  • Flavio Sartoretto
    Flavio Sartoretto about 3 years

    My code

    \documentclass[a4paper,12pt,twoside]{article}
    \usepackage[english]{babel}
    \usepackage{amssymb}
    \usepackage{amsmath}
    
    \newcommand{\QUE}[1]{%
    {\bf Question\# 1 }: #1
    }%end QUE
    
    \raggedbottom
    
    \begin{document}
    
    \section{Algorithms and data structures} 
    
    \QUE{Given the following C function definition:
    \begin{verbatim}
    int foo(int a, int b){
      if (a == b)
        return a;
      else
        return (a>b) ? foo(a-b, b) : foo(a, b-a);
    }
    \end{verbatim}
    What is the result of the computation of \texttt{foo(24, 16)}?
    }
    
    \smallskip\hrule\smallskip
    
    \end{document}
    

    Produces:

    [...]
    Runaway argument?
     int foo(int a, int b){ if (a == b) return a; else return (a>b) ? foo\ETC.
    ! File ended while scanning use of \@xverbatim.
    <inserted text> 
                    \par 
    l.74 \input{Q0201}
    etc. 
    

    If I comment out \begin{verbatim}...\end{verbatim} the code runs. Same problem with other \begin{something}...\end{something} Any hint?

    • David Carlisle
      David Carlisle about 10 years
      You can use the {} button and backticks to format code in the question (see my edit) verbatim environments can not be used in the argument to another command.
    • jon
      jon about 10 years
      My advice is to not put verbatim stuff in a general macro; verbatim requires some trickery not suited to general macros. See this question, or the very useful CTAN FAQ.
    • jon
      jon about 10 years
      See also the list of packages for code listing and syntax highlighting on CTAN, or the list for verbatim text.
    • Flavio Sartoretto
      Flavio Sartoretto about 10 years
      Thank you very much for your illumunating response, David. Actually I wanted \usepackage{listings} and use
    • Flavio Sartoretto
      Flavio Sartoretto about 10 years
      Thank you very much for your illuminating response, David. Actually I wanted \usepackage{listings} and use \begin{lstlisting} int foo(... \end{lstlisting}, in oder to format C code. Unfortunately the log says: Package Listings Warning: Text dropped after begin of listing on input line 29. (/usr/local/texlive/2009/texmf-dist/tex/latex/base/omscmr.fd‌​)) * [...] Any more hints?
  • Guess
    Guess about 10 years
    naturally, one may question the whole point of the \QUE but I imagined perhaps the OP wants to apply some additional formating like \itshape to the non-verbatim part of the question. Then it makes sense to enclose the whole thing in braces creating a group. (or perhaps adjust some line spacing parameters).
  • Flavio Sartoretto
    Flavio Sartoretto about 10 years
    Thank you very much for your response. My problem is that also using \usepackage{listings} and \begin{lstlisting} int foo(... etc. Produces analogous errors. More hints?
  • Flavio Sartoretto
    Flavio Sartoretto about 10 years
    Wonderful! Your changes to "\QUE" fix my problem. Your idea is right: after "Question\# ..." i put a counter. My simplification of \QUE was only for brevity. I did never use \bgroup. Thank you VERY MUCH!