# Using verbatim in a definition [duplicate]

I actually need to store some verbatim text in a definition so I can reuse it several times.

Here is a very short snippet:

% here I store verbatim text
\def\foo{
bar \verb'baz' spam
}
% now I reuse it
\foo


It doesn't work at all. Any idea? (\texttt is not a solution for me).

-
Thank you for the migration! I will check if the answer on top is a solution for me. –  Edouard Thiel Mar 2 '13 at 10:19

## migrated from stackoverflow.comMar 2 '13 at 3:32

This question came from our site for professional and enthusiast programmers.

## marked as duplicate by Werner, Qrrbrbirlbel, Thorsten, Marco Daniel, diabonasMar 2 '13 at 8:06

Verbatim is hard.

The way that \verb (and the verbatim environment) works is quite tricky.

Say you write \verb|\foo|. The \verb command tells TeX to regard various special characters (such as \) as ordinary letters, then it reads the 'argument' \foo as a sequence of four letters, then it changes the characters like \ back to being special (omitting various details...).

However TeX 'reads' each character only once, so when TeX reads \def\foo{bar \verb'\baz'}, it reads the entire {...} argument before it tries to evaluate it, and it reads it, crucially, with the special/not-special definitions in force at that point. Why does this matter?

You would think that, when you later expand \foo, TeX would re-read this argument, and do its \verb magic at that point. But it's too late: TeX has already done all the reading it's going to do, and decided that \ is a special character, and hence that \baz is a macro call; the value of \foo is stored as some letters 'bar ', plus a command \verb, a letter ', a command \baz, and another letter '.

So: this is why what you've written doesn't work like you expect. What you're aiming to do (text added following an explanation in the comment) is to selectively include and ignore text.

One way of doing that is using the 'comment' package in LaTeX. For example:

\documentclass{article}
\usepackage{comment}
%\includecomment{comment} % uncomment this to see the {comment} contents
\begin{document}
This is some text.
\begin{comment}
This is more text.
\end{comment}
And this is a final sentence.
\end{document}


If you search for 'latex exercises', or 'latex problem sets', or something along those lines, you might find something more tuned to your particular problem. But they'll very probably be using something like the 'comment' package's technique under the covers.

If, further, you want to be able to switch this 'comment' on and off at the command line, then you can use a technique such as the following:

\documentclass{article}
\usepackage{comment}
\else
\includecomment{comment} % uncomment this to see the {comment} contents
\fi
\begin{document}
This is some text.
\begin{comment}
This is more text.
\end{comment}
And this is a final sentence.
\end{document}


In this case (and presuming this is in a file called comm.tex), you can say pdflatex comm to get the version without the comment, and pdflatex '\def\includemycomments{x}\input{comm}' to get the version with (the odd \ifx command tests whether the \includemycomments macro is defined). This is a quite general technique for configuring the contents of a LaTeX file from the command-line.

Steven B. Segletes suggests using a box. What that looks like is the following:

\documentclass{article}
\else
\fi
\begin{document}
This is some text.

That's the general idea (in case you're wondering, the key distinction from the 'why verbatim is hard' explanation above is that the argument to \answer is read as the argument of vbox, which is not a macro, but a TeX primitive). If you want to do something more sophisticated, I'd suggest finding a package which does this – I would lay money that there's a LaTeX package for managing exercise-answer sets, and it's just a matter of google-fu to find it.