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.

Is it possible to dump the expanded form of a command as text?

E.g. when I write a macro

\newcommand{\mytest}[1]{\texttt{#1}}

I would want to define a macro \mydump such that

\mydump{\mytest{Hello World}}

creates a literal text \mytest{Hello World} in the output file.

When experimenting with custom macros, I sometimes run into rather unhelpful error messages. It would help a lot to be able to view level by level, how the problematic command is expanded.

I know that there are commands like \show, but those are command line oriented. Usually however I will be working in a LaTeX editor, that by default doesn't even show the log file, e.g. TeXStudio or Emacs with AUCTeX. So a more practical solution would be to put similar information into the output pdf file.

I tried to produce the needed output with something along the lines of combining \verb and \expandafter, but this gives no viable results (due to how LaTeX parses text into tokens and expands tokens as macros).

share|improve this question
    
I think this could help you: tex.stackexchange.com/q/4784/36686 –  Bordaigorl Sep 1 at 12:04
    
If you want to do any serious debugging of TeX code you need at least to read the log, if not to have the terminal output visible. Many TeX-focussed editors manage to combine showing the terminal stream with being GUIs. –  Joseph Wright Sep 1 at 12:27

1 Answer 1

up vote 4 down vote accepted

Here are two versions that give slightly different behavior, depending on what you really seek.

\documentclass{article}
\usepackage[T1]{fontenc}
\newcommand{\mytest}[1]{\texttt{#1}}
\def\mydump#1{\detokenize\expandafter{#1}}
\def\mydumplit#1{\detokenize{#1}}
\begin{document}
\mydump{\mytest{Hello World}}

\mydumplit{\mytest{Hello World}}
\end{document}

enter image description here

Of course, \detokenize has some limitations. You can't have % in the argument, braces must be matched, etc.

share|improve this answer
2  
all very modern and e-texy, not just \meaning ? :-) –  David Carlisle Sep 1 at 15:44
    
@DavidCarlisle Two reasons come to mind. Despite my age, I am a relative TeX newcomer (only a decade) and so am learning it backward: LaTeX first and then TeX. But secondly, I seem to recall that \meaning comes with some leading gobbledygook that may need stripping. For example, using \def\mydumpold#1{\meaning#1}, the result of \mydumpold{\mytest{Hello World}} is \long macro:#1->\texttt {#1}Hello World which may be more than the OP wants. –  Steven B. Segletes Sep 1 at 19:11

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.