# Generating code examples

I would like to make a table showing some custom commands that I have defined. It would be nice if I could do something like the following.

\documentclass{article}

\newcommand{\reals}{\ensuremath{\mathbf{R}}}
\newcommand{\CodeExample}[1]{\verb~#1~ & #1 \\}

\begin{document}

$$\begin{array}{r@{\qquad}r} \CodeExample{\reals} \end{array}$$

\end{document}


I want the left column of my table to be the LaTeX code, and the right column to be the typeset output. However, my code places the typeset output in both columns. Is there anyway to make this work, or do I need to manually enter both columns of my table?

Thanks everyone for your help! I would like to do one more thing: define an environment to generate the table of code examples. Here is my attempt.

\documentclass{article}
\usepackage{array,xparse}
\NewDocumentCommand{\CodeExample}{v}{#1 & \scantokens{#1} \\}
\newenvironment{CodeExamples}[1]
{
\begin{center}
\begin{tabular}{|>{\ttfamily}l|>{$}l<{$}|}
\hline
}
{
\hline
\end{tabular}
\end{center}
}

\newcommand{\reals}{\ensuremath{\mathbf{R}}}

\begin{document}

\begin{CodeExamples}
\CodeExample{\reals}
\CodeExample{\reals^{n}}
\end{CodeExamples}

\begin{CodeExamples}
\ \CodeExample{\reals}
\CodeExample{\reals^{n}}
\end{CodeExamples}

\end{document}


There are two problems with my environment. First, I need the ensuremath in my definition of the reals macro; otherwise, I get a "mathbf only allowed in math mode" error. Second, the environment seems to be eating the first character of the input: that is why I added the extra space in the second example of the CodeExamples environment.

-
Something like this already exists in the form of showexpl. Check out some examples of using showexpl. –  Werner Feb 7 '13 at 7:28
Related (very duplicate-y): Side-by-side source and output when documenting a style file –  Werner Feb 7 '13 at 7:31
Welcome to TeX.sx! This question is very similar to Side-by-side source and output when documenting a style file. Please take a look at it as the information there might help you. If so, that's great, and we'll probably close this question as a duplicate just to keep the place tidy and to help people find answers quickly. If not, please edit your question here to explain why so that people can better focus their attention to help you. –  Martin Schröder Feb 7 '13 at 7:42
I want to create a table of small commands; it seems as though showexpl is better suited to larger blocks of code. egreg's answer was exactly what I was looking for. –  user25619 Feb 7 '13 at 18:56

If you have a fairly recent TeX distribution, this can do for you.

\documentclass{article}
\usepackage{array,xparse}
\NewDocumentCommand{\CodeExample}{v}{#1 & \scantokens{#1}}

\newcommand{\reals}{\mathbf{R}}

\begin{document}

\begin{center}
\begin{tabular}{>{\ttfamily}l >{$}l<{$}}
\CodeExample{\reals}
\end{tabular}
\end{center}

\end{document}


The v argument type means "absorb verbatim", but then \scantokens rereads the input.

The tabular has two columns, the first one uses typewriter type, the second one is in math mode.

Never use $$ in LaTeX, see Why is $...$ preferable to$$?

I would never use \ensuremath for that symbol.

-
Thank you so much for your help! \documentclass{article} \usepackage{array,xparse} \NewDocumentCommand{\CodeExample}{v}{#1 & \scantokens{#1} \\} \newenvironment{CodeExamples}[1] { \begin{center} \begin{tabular}{|>{\ttfamily}l|>{$}l<{$}|} \hline } { \hline \end{tabular} \end{center} } \newcommand{\reals}{\ensuremath{\mathbf{R}}} \begin{document} \begin{CodeExamples} \CodeExample{\reals} \CodeExample{\reals^{n}} \end{CodeExamples} \begin{CodeExamples} \ \CodeExample{\reals} \CodeExample{\reals^{n}} \end{CodeExamples} \end{document} –  user25619 Feb 7 '13 at 18:39
Sorry that was such a mess, but I couldn't figure out how to add code in a comment, and the discussion has been closed. What I want to do is define the environment CodeExamples to generate the table of examples. I have two problems. (1) I need to use ensuremath in my reals command or I get a "mathbf only allowed in math mode" error. (2) The environment seems to be eating the first character of the input, so I have to use the hack of adding a single blankspace at the beginning. Thanks in advance for any help you can provide with these issues. –  user25619 Feb 7 '13 at 18:42
@user25619 If you have additions to make on your question, please edit it, rather than adding them in the comments where it's difficult to put much information. –  egreg Feb 7 '13 at 18:47
Ah, sorry! I'll go do that. –  user25619 Feb 7 '13 at 18:50
@user25619 I've asked to reopen the discussion. However, your \newenvironment{CodeExamples} mustn't have [1], which is the cause for the "character swallowing". –  egreg Feb 7 '13 at 18:51
show 1 more comment