2

I have the following MWE which takes an argument and prints it verbatim. I have obtained the code from this answer.

\documentclass{article}
\makeatletter
\newcommand{\myverb}{%
    \begingroup
    % deactivate special characters
    \let\do\@makeother
    \dospecials
    % change '{' and '}' back to normal
    \catcode`\{=1
    \catcode`\}=2
    \@myverb%
}
\def\@myverb#1{%
    \endgroup%
    \texttt{#1}%
}
\makeatother
\begin{document}
\myverb{$\alpha$}
\end{document}

yields $\alpha$ on the page.

I want to define a macro \showcase which will print the code that was used for the text, and the evaluation of that code, namely

\newcommand\showcase[1]{\myverb{#1} #1}
\showcase{$\alpha$}

I expected this to print

$\alpha$ <actual-letter-alpha>

but instead it prints

<actual-letter-alpha> <actual-letter-alpha>

because the argument is expanded before it is taken by \myverb (I think).

What should I do to make the macro behave the way I want?

Note: I want to use this macro either inside a table or minipages, so that I can showcase the macros I have defined. So I need to be able to play with it, like put the column delimiter & between, or separate them into two minipage environments.

osolmaz
  • 1,283
  • \newcommand\showcase[1]{\texttt{\meaning#1} #1}} – David Carlisle Jul 25 '14 at 14:53
  • The argument is not expanded, but catcode changes have no affect on tokens (which is why you can not use \verb in a macro argument) so your definition is just\texttt{#1}` – David Carlisle Jul 25 '14 at 14:54
  • @DavidCarlisle True, I also noticed curly braces are not printed. How can I achieve this then? – osolmaz Jul 25 '14 at 15:09
  • curly braces should be printed by \meaning in texttt ? – David Carlisle Jul 25 '14 at 15:18
  • @DavidCarlisle I meant in the one I defined, \myverb. – osolmaz Jul 25 '14 at 15:19
  • a better heading might be "macro to print definition code verbatim". your original title describes why you want to do something, but not what you want to do. – barbara beeton Jul 25 '14 at 15:22
  • sorry that one really isn't what you want (or fixable), the first thing is to use \meaning, then you don't have a problem with {} If you don't like eh prefix \meaning adds you can remove it (see for example the definition of verb in tabularx) – David Carlisle Jul 25 '14 at 15:22

1 Answers1

2

You need \meaning, something like

\documentclass{article}


\def\foo#1>{}

\newcommand\showcase[1]{%
\def\tmp{#1}%
\texttt{\expandafter\foo\meaning\tmp} #1}

\begin{document}

\showcase{$\alpha$}

\end{document}
David Carlisle
  • 757,742
  • I'm getting a space after \alpha. What could be the reason? I'm getting spaces after macros in general. – osolmaz Jul 25 '14 at 15:42
  • @nrs tex has no idea whether you entered $\alpha$ or $\alpha $ that information just isn't recorded. It always puts a space out so that if you entered $\alpha a$ it shows $\alpha a$ not $\alphaa$ – David Carlisle Jul 25 '14 at 15:45
  • @nrs an alternative would be to use \verb|$\alpha$| $alpha$ – David Carlisle Jul 25 '14 at 15:46
  • Excellent, thanks. Although I still wonder whether there is a way to make it verbatim and evaluate it at the same time? Is it not possible? Because it would be much better if I could also adjust the spaces. – osolmaz Jul 25 '14 at 17:18
  • You could read it verbatim and then use \scantokens but scantokens can bite, I'd use the one in the answer or the one in the comments:-) – David Carlisle Jul 25 '14 at 17:35