[Tex/LaTex] Inline LaTeX code and result of the code (similar to showexpl)

showexplverbatim

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?

Best Answer

\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}

enter image description here


@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:

enter image description here

\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}