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.

  • \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
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}
  • 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

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.