How use fancyvrb to show both result and verbatim text?

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

How?

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

\documentclass{article}
\usepackage{fancyvrb}

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

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

\begin{document}

\bigskip

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

\end{document}


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.

\documentclass{article}

\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{enumitem}
\usepackage{suffix}

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

\usepackage{cleveref}

% Tefererencing macros:

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

\newcommand{\thmnameref}[1]{\hyperref[{#1}]{\nameref{#1}}}
\WithSuffix\newcommand\thmnameref*[1]{\nameref*{#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

\swapnumbers

\declaretheoremstyle[
notefont=\bfseries,
bodyfont=\mdseries\itshape,
]{thmstyle}

\makeatletter
\declaretheoremstyle[
notefont=\bfseries, notebraces={}{},
bodyfont=\mdseries\itshape,
]{namedthmstyle}
\makeatother

\theoremstyle{thmstyle}% default
\declaretheorem[name=Theorem,numberwithin=section]{theorem}

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

\begin{document}

\section{Stuff}\label{sec:stuff}

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

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

\begin{theorem}\label{thm:another}
Another big result.
\end{theorem}

\begin{itemize}
\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}!
\end{itemize}

\smallskip

\begin{itemize}
\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}!
\end{itemize}

\smallskip

\begin{itemize}
\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}!
\end{itemize}

\smallskip

\begin{itemize}
\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}!
\end{itemize}

\end{document}

• 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. 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". Nov 30, 2016 at 19:30

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.

\documentclass{article}
\usepackage{fancyvrb}
\makeatletter
%%----------------------------------------------------------------------
%% 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:
%%
%% (\romannumeral expansion was introduced in order to overcome the
%% concerns and worries about improperly balanced \if..\else..\fi constructs.)
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\newcommand\UD@CheckWhetherNull[1]{%
\romannumeral0\expandafter\UD@secondoftwo\string{\expandafter
\UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
\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.
%
\begingroup
\catcode\^^M=12 %
\UD@firstoftwo{%
\endgroup%
\newcommand\UDEndlreplace[2]{\romannumeral0\@UDEndlreplace{#2}#1^^M\relax{}}%
\newcommand*\@UDEndlreplace{}%
\long\def\@UDEndlreplace#1#2^^M#3\relax#4#5{%
\UD@CheckWhetherNull{#3}%
{ #5{#4#2}}{\@UDEndlreplace{#1}#3\relax{#4#2#1}{#5}}%
}%
}{}%
\newcommand\UDcollectOneVerbArg{\@UDOneVerbArg{\@UDcollectOneVerbArg}}%
\newcommand\@UDOneVerbArg[3]{%
\@bsphack
\begingroup
\let\do\@makeother\dospecials
\catcode\{=1 %
\catcode\ =10 %
\@ifnextchar\bgroup
{\catcode\}=2 \@@UDOneVerbArg{#1}{#2}{#3}{}}%
{\do\{\@@UDOneVerbArg{#1}{#2}{#3}}%
}%
\newcommand\@@UDOneVerbArg[4]{%
\do\ %
\catcode\^^M=12 %
\long\def\@tempb##1#4{%
\edef\@tempb{##1}%
\@onelevel@sanitize\@tempb
\expandafter\UDEndlreplace\expandafter{\@tempb}{#2}{\def\@tempb}%
\expandafter#1\expandafter{\@tempb}{#3}{#4}%
}%
\@tempb
}%
\newcommand\@UDcollectOneVerbArg[3]{%
\endgroup
\@esphack
#2{#3}{#1}%
}%
% Implementation of generic stuff for reading arguments "verbatim" done.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand\showboth{\UDcollectOneVerbArg{^^J}{\innershowboth}}%
\newcommand\innershowboth[2]{%
\UD@CheckWhetherNull{#1}%
{\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}}%
\endgroup
}

\makeatother

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

\begin{document}

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

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

• 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.) 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. 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. 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. 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. Nov 30, 2016 at 23:12