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.

To write some documentation, I would like the ability to define a command such as

\showcase{x^{y}\in\Omega}

which expands to something roughly equivalent to

\verb|x^{y}\in\Omega| & $x^{y}\in\Omega$ \\

so that I can show what the latex code looks like and how does it show on the document.

Now I now that it isn't really possible to use \verb for this because it's really fragile and you can't pass arguments from a macro to it. But, assuming that I'm not trying to write anything crazy in the argument to \showcase (e.g. braces are always balanced, I don't care much if spaces are collapsed, no %'s inside), is there a way to define such a command?

Update

\detokenize does (almost) exactly what I want. Take the following example

\newcommand\showcase[1]{{\ttfamily\detokenize{#1}} & $#1$}
\begin{tabular}{cc|cc}
\showcase{x^{y}}      & \showcase{\hat{x}, \bar{x}} \\
\showcase{x_{y}}      & \showcase{f\colon X \to Y} \\
\showcase{x'}         & \showcase{\sqrt{x+2}} \\
\showcase{x''_{2}}    & \showcase{2 < x \leq 4} \\
\showcase{A^{1}_{2}}  & \showcase{\frac{a+b}{c+d}} \\
\showcase{3\pi/4}     & \showcase{\int_{0}^{1} x^{2} \,dx} \\
\showcase{x\in\Omega} & \showcase{A \cup B \subseteq C \cap D}
\end{tabular}

Which produces the output:

enter image description here

My minor quibbles are that:

  • The double '' seems to have been collapsed into a "
  • I find mildly annoying the spaces after control sequences. I know why they are there, but could there be a way to remove the space if it is not followed by a letter or number? (Or, conversely, if followed by { or _)
share|improve this question
    
\detokenize might work for you, if you don't have % or unbalanced expressions in the verbatim material. –  Steven B. Segletes Aug 15 '13 at 16:37
    
Related (duplicate?): tex.stackexchange.com/questions/19295/… (see also the linked questions) –  Torbjørn T. Aug 15 '13 at 16:40
    
@TorbjørnT. Correct me if I'm wrong but, looking at those packages, they seem to provide example “environments”, I'm looking more for some small almost “inline” code usage (e.g. to make a table). / I'm looking at detokenize now, it (almost!) works, but it has some small problems, will update my question soon. –  Juan A. Navarro Aug 15 '13 at 16:49
    
No, you're probably right, I haven't looked much at them myself. –  Torbjørn T. Aug 15 '13 at 17:35
    
For the spaces/detokenize stuff, see Detokenizing without extra spaces?. On the '' business, this is because of the font ligature. You probably want to look \@noligs from the LaTeX kernel. –  Joseph Wright Aug 15 '13 at 18:40

2 Answers 2

up vote 7 down vote accepted

For math mode this should work.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\showcase}{v}
 {
  \texttt{#1} & \tl_rescan:nn { } { $#1$ }
 }
\ExplSyntaxOff

\begin{document}
\begin{tabular}{cc|cc}
\showcase{x^{y}}      & \showcase{\hat{x}, \bar{x}} \\
\showcase{x_{y}}      & \showcase{f\colon X \to Y} \\
\showcase{x'}         & \showcase{\sqrt{x+2}} \\
\showcase{x''_{2}}    & \showcase{2 < x \leq 4} \\
\showcase{A^{1}_{2}}  & \showcase{\frac{a+b}{c+d}} \\
\showcase{3\pi/4}     & \showcase{\int_{0}^{1} x^{2} \,dx} \\
\showcase{x\in\Omega} & \showcase{A \cup B \subseteq C \cap D}
\end{tabular}
\end{document}

enter image description here

share|improve this answer
1  
Very nice! I knew there had to be a simple LaTeX3 answer, but couldn't figure it out. –  Juan A. Navarro Aug 15 '13 at 22:20

I created \detokenizeplus which detokenizes but removes spaces before left braces and underscores, which were the problems for your examples. Additionally, I remove the space between consecutive macro names (but can only detect the first macro if there is space before its name). The place where that should apply is in between \in\Omega. However, because \in was immediately preceded byx and not a space, my logic can't catch that case. So for this MWE, I made the argument x \in\Omega to test that I could trap consecutive macros.

The logic here is that \getargsC breaks up the input into space separated arguments (\argi, \argii, \argiii, etc.) which can be exploited to determine whether to add back a space or not when regurgitating the arguments sequentially.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{stringstrings}
\usepackage{readarray}
\newcommand\showcase[1]{{\ttfamily\detokenizeplus{#1}} & $#1$}
\def\firstchar#1#2|{#1}
% catcode=12 BACKSLASH
\edef\tbs{\detokenize{\X}}
\edef\tbs{\expandafter\firstchar\tbs|}
% catcode=12 LEFT BRACE
\edef\tlb{\detokenize{{}}}
\edef\tlb{\expandafter\firstchar\tlb|}
% catcode=12 UNDERSCORE
\edef\tus{\detokenize{_}}
\newcounter{index}
\newcommand\detokenizeplus[1]{%
  \def\temparg{\detokenize{#1}}%
  \getargsC{\temparg}%
  \setcounter{index}{0}%
  \def\prevmacro{F}%
  \whiledo{\value{index} < \narg}{%
    \stepcounter{index}%
%   DETERMINE IF A MACRO; REMOVE SPACE BETWEEN CONSECUTIVE MACROS
    \isnextbyte[q]{\tbs}{\csname arg\roman{index}\endcsname}%
    \if T\theresult%
      \if T\prevmacro\unskip\else\fi%
      \def\prevmacro{T}%
    \else%
      \def\prevmacro{F}%
   \fi%
%   REMOVE SPACES BEFORE LEFT BRACE
    \isnextbyte[q]{\tlb}{\csname arg\roman{index}\endcsname}%
    \if T\theresult\unskip\else\fi%
%   REMOVE SPACES BEFORE UNDERSCORES
    \isnextbyte[q]{\tus}{\csname arg\roman{index}\endcsname}%
    \if T\theresult\unskip\else\fi%
    \csname arg\roman{index}\endcsname~%
  }%
}
\begin{document}

\begin{tabular}{cc|cc}
\showcase{x^{y}}      & \showcase{\hat{x}, \bar{x}} \\
\showcase{x_{y}}      & \showcase{f\colon X \to Y} \\
\showcase{x'}         & \showcase{\sqrt{x+2}} \\
\showcase{x''_{2}}    & \showcase{2 < x \leq 4} \\
\showcase{A^{1}_{2}}  & \showcase{\frac{a+b}{c+d}} \\
\showcase{3\pi/4}     & \showcase{\int_{0}^{1} x^{2} \,dx} \\
\showcase{x \in\Omega} & \showcase{A \cup B \subseteq C \cap D}
\end{tabular}

\end{document}

enter image description here

share|improve this answer
    
Wow! I'm absolutely amazed. Thanks a lot! –  Juan A. Navarro Aug 15 '13 at 20:16
1  
You can also use \prime instead of ' –  John Kormylo Aug 15 '13 at 21:00

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.