Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 11 down vote accepted
\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}
share|improve this answer
    
Excellent! Is there any way to remove the spaces? –  user937840 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. –  user937840 Aug 2 '12 at 17:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.