3

Is it be possible to display a table with both typeset math expressions and verbatim renderings the corresponding input for those expressions?

For example, something like the first two lines of output shown below, but within a tabular or align environment. (The last two lines of the output give almost what I want, but of course lack display of /.)

Displaying input and output of math expressions

The source for the above is:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\noindent
$X \vee Y$ \qquad \verb!X \vee Y!
\\
$X \wedge Y$ \qquad \verb!X \wedge Y!

\noindent
\begin{align*}
X \wedge Y &\qquad \texttt{X  wedge Y}
\\
X \vee Y &\qquad \texttt{X vee Y}
\end{align*}

\end{document}

Of course I am aware of how to do this when just text is involved, as considered, e.g., in: How use fancyvrb to show both result and verbatim text?.

A mathemartical aside

Somewhat confusingly, the symbol \vee is used in topology to denote the wedge sum, whereas the symbol \wedge is not (it's used for the smash product).

  • While you can get verbatim into an align, the easist would probably be to stick with tt text, as in \texttt{X \char92 wedge Y} – Steven B. Segletes Apr 9 '20 at 19:49
  • You can do \texttt{X \string\wedge\ Y} and \texttt{X \string\vee\ Y}. – Werner Apr 9 '20 at 19:49
  • Drat, I forgot about the \string command! – murray Apr 9 '20 at 19:58
  • 1
    The problem is that align* absorbs the text up to \end{align*} as the argument to a macro, so \verb is not allowed. – egreg Apr 9 '20 at 20:00
3

You can write some code to format both input and output at the same time:

enter image description here

\documentclass{article}

\usepackage{amsmath}

\makeatletter
\newcommand{\mathandcode}[1]{%
  \mbox{$#1$} &
    \mbox{\ttfamily \detokenize{#1}}% https://tex.stackexchange.com/a/139980/5764
}

\begin{document}

\begin{tabular}{ l l }
  \mathandcode{X \vee Y} \\
  \mathandcode{X \wedge Y}
\end{tabular}

\end{document}
  • Perhaps the simplest yet! – murray Apr 11 '20 at 20:20
4

You could use a tabular as shown in the following example:

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\begin{document}

\begin{tabular}{>{$}l<{$}l}
X \vee Y & \verb!X \vee Y!
\\
X \wedge Y & \verb!X \wedge Y!
\end{tabular}

\end{document}
  • This way seems a lot simpler than having to use \string. – murray Apr 9 '20 at 19:59

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.