# Inline LaTeX code and result of the code (similar to showexpl)

I would like to write a macro that works somewhat like showexpl but just reproduces the LaTeX code and the results of the code in a way that I can use inline.

\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
I want a macro that produces something like \verb|$\bar\mathbf{x}$| produces $\bar\mathbf{x}$''.
\end{document}


Is this possible?

\documentclass{article}
\usepackage{fancyvrb}

\def\showvrb#1{%
\texttt{\detokenize{#1}} produces #1''%
}

\begin{document}
I want a macro that produces something like \verb|$\bar\mathbf{x}$| produces $\bar\mathbf{x}$''.

I want a macro that produces something like \showvrb{$\bar\mathbf{x}$}.
\end{document}


@Werner made a very similar answer (deleted) to the \detokenize version and at his suggestion I'll add some of the additional comments he made there:

Note though that \detokenize returns the tokens used in an "already-grabbed" argument, and therefore introduces spaces. Also, you won't be able to pass \verb or verbatim content as a macro argument (see Why doesn’t verbatim work within …?). There might be other things that will break this as well.

An alternatve approach is to first read the argument verbatim and then use \scantokens to re-parse it to obtain the normal meaning:

\documentclass{article}
\usepackage{fancyvrb}

\makeatletter

\def\showvrbb{\begingroup\let\do\@makeother \dospecials\showvrbbx}
\makeatother

\def\showvrbbx#1{%
\def\tmp##1#1{%
\texttt{##1} \endgroup produces \scantokens{##1}''}%
\tmp}

\begin{document}
I want a macro that produces something like \verb|$\bar\mathbf{x}$| produces $\bar\mathbf{x}$''.

I want a macro that produces something like \showvrbb|$\bar\mathbf{x}$|.
\end{document}

• Excellent! Is there any way to remove the spaces? – tgkolda Aug 1 '12 at 23:37
• yes/no it depends. if you start off with #1 then if you use \scantokens (or \meaning) you can't tell if there were spaces initially so if you remove spaces from this example they would also go if the input was \bar \mathbf {x} an alternative is to read the argument verbatim and then use \scantokens to get the typeset version. Might add that later (but it's late here:-) – David Carlisle Aug 1 '12 at 23:44
• Both approaches are working great. I will post here if I encounter any problems. – tgkolda Aug 2 '12 at 17:41
• One downside with the \scantokens approach is that it tends to replace spaces with \textvisiblespaces (the things that look like a squished "U"). This is because \let\do@makeother \dospecials maps all the special characters, including space, into the "other" category. You can omit space from that list by defining \def\mostspecials{\do \\\do \{\do \}\do \\$\do \&\do \#\do \^\do \_\do \%\do \~}, then using \mostspecials instead of \dospecials in the \showvrbb definition. Disclaimer: I do not fully understand how category codes work, but this worked for me. – Ray Oct 5 '16 at 23:51