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

4
  • 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} Commented Apr 9, 2020 at 19:49
  • You can do \texttt{X \string\wedge\ Y} and \texttt{X \string\vee\ Y}.
    – Werner
    Commented Apr 9, 2020 at 19:49
  • Drat, I forgot about the \string command!
    – murray
    Commented Apr 9, 2020 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
    Commented Apr 9, 2020 at 20:00

2 Answers 2

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}
1
  • Perhaps the simplest yet!
    – murray
    Commented Apr 11, 2020 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}
1
  • This way seems a lot simpler than having to use \string.
    – murray
    Commented Apr 9, 2020 at 19:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .