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 am looking for a command similar to show, but want the output in the document itself. All the examples I have found produce the output in the terminal, not the document.

So, I want to replace the use of \show with an appropriate macro in the following:

\documentclass{article}

\newcommand*{\MyFrac}{\ensuremath{\frac{\pi}{4}}}

\begin{document}
The macro \textbackslash MyFrac is set to \show\MyFrac.
\end{document}

and the output I want produced is

The macro \MyFrac is set to \ensuremath{\frac{\pi}{4}}

I could probably use the verbatim package, but I think that would require me to change the definition at two places if it was to be updated. I want to be able to define the macro in one place and be able use it as needed, but also to be able to display the LaTeX code associated with it.

share|improve this question
add comment

2 Answers

up vote 17 down vote accepted

To a zeroth approximation, you could use \meaning:

The macro \textbackslash MyFrac is set to {\ttfamily\meaning\MyFrac}

yields

The macro \MyFrac is set to macro:->\ensuremath {\frac {\pi }{4}}.

If you know you only call it on macros anyway, you can probably extend it to strip the macro:-> bit (which will also list the parameters etc) like so, using a LaTeX kernel command:

\makeatletter
\newcommand\meaningbody[1]{%
  {\ttfamily
    \expandafter\strip@prefix\meaning#1}%
}
\makeatother

\begin{document}
The macro \textbackslash MyFrac is set to \meaningbody\MyFrac.
\end{document}
share|improve this answer
    
Excellent. This is exactly what I needed. Just curious as to why ttfamily was required. I see that it doesn't work without that. –  Peter Grill Jun 10 '11 at 5:06
2  
@Peter It works, but the text fonts in the OT1 encoding have strange characters in place of \, <, > and others. –  egreg Jun 10 '11 at 6:13
    
@egreg No problem with T1 (why OT1 ?) but it's better to show the code with a different font. I think \ttfamily is the common usage. –  Alain Matthes Jun 10 '11 at 6:49
    
@Altermundus People in the UK or the US are not forced to use T1 encoded fonts as we continental European do. Of course \ttfamily is better than normal text font and it's necessary with OT1 fonts because > gives ¡ in the Roman font. Ouch! –  egreg Jun 10 '11 at 15:11
add comment

You can also avoid \textbackslash with

\usepackage{shortvrb}
\AtBeginDocument{\MakeShortVerb{\|}} 

and now you can write |\MyFrac|. It's possible to make another choice for the delimiter's character

share|improve this answer
2  
You can write |\MyFrac| sometimes. You had better not use it in fragile commands such as sectioning commands, caption, etc. Verbatim is very fragile. In fact, it is worse than fragile. Because veratim mucks with category codes, one should never use verbatim in the argument of any command. Period. –  David Hammen Jun 10 '11 at 12:01
add comment

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.