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

E.g. when I write a macro


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).

  • I think this could help you: tex.stackexchange.com/q/4784/36686 – Bordaigorl Sep 1 '14 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 '14 at 12:27

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

\mydump{\mytest{Hello World}}

\mydumplit{\mytest{Hello World}}

enter image description here

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

  • 2
    all very modern and e-texy, not just \meaning ? :-) – David Carlisle Sep 1 '14 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 '14 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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