9

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?

1 Answer 1

12
\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}
4
  • 1
    Excellent! Is there any way to remove the spaces?
    – tgkolda
    Commented Aug 1, 2012 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:-) Commented Aug 1, 2012 at 23:44
  • Both approaches are working great. I will post here if I encounter any problems.
    – tgkolda
    Commented Aug 2, 2012 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
    Commented Oct 5, 2016 at 23:51

You must log in to answer this question.

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