Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

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).

share|improve this question

migrated from stackoverflow.com Mar 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, diabonas Mar 2 '13 at 8:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
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

1 Answer 1

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.


Edited, following comments, to add:

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}
\expandafter\ifx\csname includemycomments\endcsname\relax
\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}
\newbox\answerbox
\def\answer{\setbox\answerbox=\vbox}
\newif\ifshowanswer
\expandafter\ifx\csname includemycomments\endcsname\relax
  \showanswerfalse
\else
  \showanswertrue
\fi
\begin{document}
This is some text.
\answer{This is \verb|verb\ati~m| text}
And this is a final sentence.
\ifshowanswer
\box\answerbox
\fi
\end{document}

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.

share|improve this answer
    
I need it to write once an exercise with its solution; then output the exercise without the solution, then again with the solution. I have a lot of exercises with a lot of verbatim inside to process. I think it is possible since it works in beamer (when setting [containsverbatim]). –  Edouard Thiel Mar 1 '13 at 21:24
    
@EdouardThiel Why don't you put the exercise into a box and the solution into a box. Then, you can call back the exercise twice, without redefining it, merely with a \usebox. –  Steven B. Segletes Mar 2 '13 at 12:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.