The documentation for fancyvrb claims: "you can build example environments (showing both result and verbatim text)...."


For example, how should commands from fancyvrb be used to construct a command such as \showboth, shown in the "toy example" below, so that the output is what is shown as "desired" rather than the "actual" output I get?

Toy example


\newcommand{\showboth}[1]{{#1} --- \protect\Verb!#1!}

\newcommand{\doit}[1]{also {#1}}


Actual output: \quad \showboth{\doit{this}}


Desired output: \quad \doit{this} --- \verb!\doit{this}!


How show result of command and its verbatim text

Actual application

In the actual application, I need such a command \showboth that can be used with an argument that comes from the argument passed by a surrounding command.

The purpose of this is to allow me to experiment systematically with various kinds of styling in my document design. For example, I want to see readily which of various styles I prefer for cross-referencing theorems and sections, as in the following source.

In that source, the package showboth that's loaded consists simply of the code in the answer by @Ulrich Diez.

What I'd like is a 1-argument macro, whose argument is a label (thm:big, e.g.), that produces the kind of complete itemize environment shown (whose individual items in turn just call \showboth with the various referencing commands, with those referencing commands in turn taking that label as their own argument.



\usepackage{showboth} % code from Ulrich Diez's answer


% Tefererencing macros:

\newcommand{\thmref}[1]{\nameref*{#1} (\cref{#1})}


\newcommand*{\fullref}[1]{\hyperref[{#1}]{\cref{#1}} \nameref*{#1}}

\newcommand*{\unitref}[1]{\cref{#1} (\nameref*{#1})}
\newcommand*{\unitnameref}[1]{\hyperref[{#1}]{\cref{#1}} (\nameref*{#1})}

% Thorerem styles



notefont=\bfseries, notebraces={}{},

\theoremstyle{thmstyle}% default

\declaretheorem[style=namedthmstyle, name=Theorem,title = {},numberlike=theorem]{namedtheorem}



\begin{theorem}[refname=Theorem,name=Preliminary Result]\label{thm:prelim}
Notice this!

\begin{namedtheorem}[name=The Big Theorem]\label{thm:big}
Very important!

Another big result.

\item  \showboth!\thmref{thm:big}! 
\item \showboth!\thmnameref{thm:big}! 
\item \showboth!\thmnameref*{thm:big}!
\item \showboth!\fullref{thm:big}! % LITTLE USE!
\item \showboth!\nameref{thm:big} \cref{thm:big}!
\item \showboth!\nameref*{thm:big} \cref{thm:big}! 
\item \showboth!\cref{thm:big}! 
\item \showboth!\cref*{thm:big}!


\item  \showboth!\thmref{thm:prelim}! 
\item \showboth!\thmnameref{thm:prelim}! 
\item \showboth!\thmnameref*{thm:prelim}!
\item \showboth!\fullref{thm:prelim}! % LITTLE USE!
\item \showboth!\nameref{thm:prelim} \cref{thm:prelim}!
\item \showboth!\nameref*{thm:prelim} \cref{thm:prelim}! 
\item \showboth!\cref{thm:prelim}! 
\item \showboth!\cref*{thm:prelim}!


\item  \showboth!\thmref{thm:another}! 
\item \showboth!\thmnameref{thm:another}! 
\item \showboth!\thmnameref*{thm:another}!
\item \showboth!\fullref{thm:another}! % LITTLE USE!
\item \showboth!\nameref{thm:another} \cref{thm:another}!
\item \showboth!\nameref*{thm:another} \cref{thm:another}! 
\item \showboth!\cref{thm:another}! 
\item \showboth!\cref*{thm:another}!


\item \showboth!\unitnameref{sec:stuff}!
\item \showboth!\unitref{sec:stuff}!
\item  \showboth!\thmref{sec:stuff}! 
\item \showboth!\thmnameref{sec:stuff}! 
\item \showboth!\thmnameref*{sec:stuff}!
\item \showboth!\fullref{sec:stuff}! % LITTLE USE!
\item \showboth!\nameref{sec:stuff} \cref{sec:stuff}!
\item \showboth!\nameref*{sec:stuff} \cref{sec:stuff}! 
\item \showboth!\cref{sec:stuff}! 
\item \showboth!\cref*{sec:stuff}!

  • 1
    Related? tex.stackexchange.com/questions/128399/… Commented Nov 30, 2016 at 11:15
  • Neither answer at tex.stackexchange.com/questions/128399/… does not do what I need: the first answer there (using stringstrings) gives a spurious space between \doit and {this}; both the first and second seem to treat the result of the command as math, thus typesetting \doit{this} as also this.
    – murray
    Commented Nov 30, 2016 at 15:20
  • Too late to edit my preceding, confusing comment to correct syntax. In first line, "does not do" should have been "does".
    – murray
    Commented Nov 30, 2016 at 19:30

1 Answer 1


Let me say in advance that I am not familiar at all to the fancyvrb package as by now I never used it.

Therefore it is unknown to me whether that package already provides some infrastructure for achieving what you desire.

Off the cuff I suggest having \showboth read its argument under "verbatim catcode régime" and then having \showboth pass that argument to another macro \innershowboth which in turn will pass that argument to \scantokens—once as is and once along with tokens for calling the \Verb-command.

I just wrote a macro \UDcollectOneVerbArg which does read arguments under "verbatim catcode régime" and then pass these arguments and the verbatim-delimiter to other macros.
I will happily use it in the example below for implementing \showboth.

Like the commands \verb (from the LaTeX-kernel) and \Verb (from the fancyvrb-package) the command \showboth needs to read its argument under "verbatim catcode régime". Therefore like the commands \verb and \Verb the command \showboth does not work with arguments that are passed by other macros/that come from macro definitions where those arguments were tokenized under normal catcode régime. Best practise for the unexperienced is to use \showboth<verbatim-Arg> not within macro definitions and not within macro arguments.

enter image description here

%% Check whether argument is empty:
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
%% (\romannumeral expansion was introduced in order to overcome the 
%% concerns and worries about improperly balanced \if..\else..\fi constructs.)
  \UD@secondoftwo\string}\expandafter\expandafter\UD@firstoftwo{ }{}%
  \UD@secondoftwo}{\expandafter\expandafter\UD@firstoftwo{ }{}\UD@firstoftwo}%
% // Snippet written by Ulrich Diez on November 30, 2016
% Implement generic stuff for reading arguments "verbatim":
% Syntax of \UDcollectOneVerbArg
%   \UDcollectOneVerbArg{<^^M-replacement>}{<mandatory>}<delimiter of verbatim arg><verbatim arg><delimiter of verbatim arg>
%   yields:
%   <mandatory>{<delimiter of verbatim arg>}{<verbatim arg>}
%   \UDcollectOneVerbArg{<^^M-replacement>}{<mandatory>}{<verbatim arg>}
%   yields:
%   <mandatory>{}{<verbatim arg>}
% with each character ^^M (usually=\endline-char) in <verbatim arg>
% replaced by token-sequence <^^M-replacement>.
% If reading <^^M-replacement> and <mandatory> from input is necessary,
% they will be read under unchanged catcode regime.
% The <verbatim arg> is also mandatory.
% It will be read under verbatim-catcode-conditions.
% There must be a leading character in front of it.
% If that leading character is an opening brace, it will be
% "assumed" that the <verbatim arg> is nested into braces.
% Otherwise it will be "assumed" that the <verbatim arg> is
% delimited by that leading character---which implies a
% syntax like with the argument of \verb.
% Empty-lines will not be ignored.
% <delimiter of verbatim arg> will be read under verbatim-catcode-conditions
% if present.
\catcode`\^^M=12 %
    { #5{#4#2}}{\@UDEndlreplace{#1}#3\relax{#4#2#1}{#5}}%
  \catcode`\{=1 %
  \catcode`\ =10 %
  {\catcode`\}=2 \@@UDOneVerbArg{#1}{#2}{#3}{}}%
  \do\ %
  \catcode`\^^M=12 %
% Implementation of generic stuff for reading arguments "verbatim" done.

  {\scantokens{{#2} --- \Verb{#2}\begingroup\catcode`\X=14 X}}%<-This will raise an error as \Verb does not accept opening braces as delimiter.
  {\scantokens{{#2} --- \Verb#1#2#1\begingroup\catcode`\X=14 X}}%


\newcommand{\doit}[1]{also {#1}}


\noindent\Verb|\doit{this  and that} --- \Verb!\doit{this  and that}!| yields:\medskip\\
\null\quad \doit{this  and that} --- \Verb!\doit{this  and that}!

\noindent\Verb|\showboth!\doit{this  and that}!| yields:\medskip\\    
\null\quad \showboth!\doit{this  and that}!

  • I strong suspected an answer would be non-trivial, but I hardly expected it to be so complicated! I still wonder what the fancyvrb docs had in mind when they said, ""you can build example environments (showing both result and verbatim text)...." (I sure wish package documentation did a consistently better job of including examples and not just explicate all the principles involved.)
    – murray
    Commented Nov 30, 2016 at 20:14
  • @murray As I wrote in my answer: I don't know much about the fancyvrb package. Therefore I don't know if an answer has to be so complicated. I did it the way I did it because I use my \UD...-macros all the time. Thus providing my answer was more or less a seven minute copy-paste job with minor modifications. Going through the manual and the code of the fancyvrb package would have taken me more time. Therefore I must lay emphasis on the fact that there might be a much less complex answer in terms of macros/infrastructure provided by the fancyvrb package. Commented Nov 30, 2016 at 20:59
  • Alas, in the actual situation, I need such a command \showboth that can be used with an argument that comes from the argument passed by a surrounding command.
    – murray
    Commented Nov 30, 2016 at 22:36
  • @murray Perhaps with the surrounding command you can play the same game as I played when implementing \showboth: Take the surrounding command for a wrapper that calls \UDcollectOneVerbArg which in turn reads arguments verbatim and passes them to yet another macro which in turn feeds the arguments along with whatsoever other tokens to \scantokens for re-tokenization... By the way: Don't hesitate to tell about the actual situation whenever asking for help. That way help can be adjusted to the actual situation ab ovo which often saves time - eh - in the long term. Commented Nov 30, 2016 at 22:59
  • @murray Or you just try something like ...\scantokens{\showboth!<whatsoever macro argument>!\begingroup\endlinechar=-1\relax}\endgroup... within the definition of the surrounding command - without having TeX read things verbatimized first. But without prior verbatimization, this does double hashes (#) and it does also attach spaces behind control words. (\scantokens is like unexpanded writing to an external file and \input-ting that external file.) Input-characters in ^^-notation might also not yield what you wish without prior verbatimization. Commented Nov 30, 2016 at 23:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .