7

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.

8

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.

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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