# Generate math symbols from argument

I have been trying to create a small latex math symbol reference. My list will require repetition of lines like \textbackslash lfloor - $\lfloor$. I want to create a command that takes in one argument that is either the symbol name lfloor or the command itself \lfloor. But in either scenario, I have trouble showing the other case. How can I define such command?

• see the source of other math symbol lists eg ctan.org/pkg/maths-symbols basically \def\foo#1{\texttt{\string#1} $#1$} ...\foo\rightarrow – David Carlisle Jun 25 '18 at 13:27
• Look up \verb. The first and last characters after it are used as delimiters and not displayed. e.g \verb|\lfloor| – John Kormylo Jun 25 '18 at 13:27
• The command you are looking for is \string. Nevertheless, the approach suggested by David Carlisle in the reference he quote, tough based on \string, is ore powerfull in the sense that it avoid to repeat the command to be illustrated. – Jhor Jun 25 '18 at 15:40

If it is only about single control sequence tokens and if eTeX-extensions are available, you can probably use a combination of \string, \scantokens and the \verb-command:

\documentclass{article}

\newcommand\myCsPrintCommand[1]{%
\expandafter\InnermyCsPrintCommand\expandafter{\string#1}%
}%
\begingroup
\makeatletter
\catcode\?=14\relax
\catcode\%=12\relax
\@firstofone{?
\endgroup
\newcommand\InnermyCsPrintCommand[1]{?
\scantokens\expandafter{\string\verb|#1| -- $#1$%}?
}?
}%
% The trickery above is needed for bringing a %-char/comment-char
% into \scantokens' argument. That in turn is needed for neutralizing
% the endline-character inserted at the end of \scantokens' argument.

\begin{document}
\myCsPrintCommand{\lfloor}
\myCsPrintCommand{\rfloor}
\end{document}


In my opinion this is sort of overkill, but I nonetheless decided to show the underlying \scantokens-trickery as it can be used not only with \verb but with other commands also whose arguments usually should not be delivered via macros but via reading and tokenizing input as these arguments should be read/tokenized under different catcode-régime, e.g. \lstinline from the listings-package, or whatever else is to be used for pretty-printing the control words.

Nonetheless the following example is my attempt at providing a generic macro \myCsPrintCommand which lets you format the pretty-printing of a control sequence with commands like \verb or \lstinline as well:

\documentclass{article}
\usepackage{color, listings}

%%----------------------------------------------------------------------
%% Both pretty-print a control sequence and typeset the result
%% from carrying out the control sequence:
%%----------------------------------------------------------------------
%% \myCsPrintCommand{<stringified-printing-command>}%
%%                  {<verb-delimiter>}%
%%                  {<separator>}%
%%                  {<formatting-when-carrying-out-command>}%
%%                  {<control sequence to typeset>}%
%%
%% ->
%% either
%%   <stringified-printing-command><verb-delimiter><control sequence to typeset><verb-delimiter>%
%%   <separator><formatting-when-carrying-out-command>{<control sequence to typeset>}
%% or
%%   <stringified-printing-command>{<control sequence to typeset>}%
%%   <separator><formatting-when-carrying-out-command>{<control sequence to typeset>}
%%
%% <stringified-printing-command> prints the character-sequence that
%% forms the control sequence.
%% <stringified-printing-command> either can be something with \verb-syntax -
%% in this case you need to specify a <verb-delimiter>, e.g., |, or
%% can be something that does process one undelimited argument -
%% in this case you need to leave the <verb-delimiter>-argument empty.
%%
%% <formatting-when-carrying-out-command> influences how the result from
%% carrying out <control sequence to typeset> gets typeset.
%% <formatting-when-carrying-out-command> can be a control sequence that
%% processes one undelimited argument.
%%
%% e.g., \myCsPrintCommand{\verb}{|}{--}{\textbf}{\LaTeX}
%%       -> \verb|\LaTeX|--\textbf{\LaTeX}
%% e.g., \myCsPrintCommand{\verb}{|}{ -- }{\ensuremath}{\lfloor}
%%       -> \verb|\lfloor| -- \ensuremath{\lfloor}
%% e.g., \myCsPrintCommand{\myownstringifier}{}{--}{\ensuremath}{\lfloor}
%%       -> \myownstringifier{\lfloor}--\ensuremath{\lfloor}
%%
\newcommand\myCsPrintCommand[5]{%
\expandafter\InnermyCsPrintCommand\expandafter{\string#5}{#1}{#2}{#3}{#4}%
}%
\begingroup
\makeatletter
\catcode\?=14\relax
\catcode\%=12\relax
\@firstofone{?
\endgroup
??----------------------------------------------------------------------
?? Expandably within two expansion steps check whether argument is empty:
??----------------------------------------------------------------------
?? \UDCheckWhetherNull{<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:
??......................................................................
\newcommand\UDCheckWhetherNull[1]{?
\romannumeral0\expandafter\@secondoftwo\string{\expandafter
\@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
\@secondoftwo\string}\expandafter\@firstoftwo\expandafter{\expandafter
\@secondoftwo\string}\expandafter\expandafter\@firstoftwo{ }{}?
\@secondoftwo}{\expandafter\expandafter\@firstoftwo{ }{}\@firstoftwo}?
}?
\newcommand\InnermyCsPrintCommand[5]{?
\UDCheckWhetherNull{#3}{?
\scantokens\expandafter{\string#2{#1}#4#5{#1}%}?
}{?
\scantokens\expandafter{\string#2#3#1#3#4#5{#1}%}?
}?
}?
}%
% The trickery above is needed for bringing a %-char/comment-char
% into \scantokens' argument. That in turn is needed for neutralizing
% the endline-character inserted at the end of \scantokens' argument.

\begin{document}

%% \myCsPrintCommand{<stringified-printing-command>}%
%%                  {<verb-delimiter>}%
%%                  {<separator>}%
%%                  {<formatting-when-carrying-out-command>}%
%%                  {<control sequence to typeset>}%

Example with \verb|\verb|:

\myCsPrintCommand{\verb}{|}{ -- }{\ensuremath}{\lfloor}

\myCsPrintCommand{\verb}{|}{ -- }{\ensuremath}{\rfloor}

Example with \verb|\lstinline|:

\myCsPrintCommand{\lstinline[language={[LaTeX]TeX}]}{|}{ -- }{\ensuremath}{\lfloor}

\myCsPrintCommand{\lstinline[language={[LaTeX]TeX}]}{|}{ -- }{\ensuremath}{\rfloor}

Example with \verb|\mybluestringified|:

\newcommand\mybluestringified[1]{%
\textcolor{blue}{\texttt{\string#1}}%
}%

\myCsPrintCommand{\mybluestringified}{}{ -- }{\ensuremath}{\lfloor}

\myCsPrintCommand{\mybluestringified}{}{ -- }{\ensuremath}{\rfloor}

% You can put the call to \myCsPrintCommand into a macro:

\newcommand\printitwithverb{%
\myCsPrintCommand{\verb}{|}{ -- }{\ensuremath}%
}%
\newcommand\printitwithlstinline{%
\myCsPrintCommand{\lstinline[language={[LaTeX]TeX}]}{|}{ -- }{\ensuremath}%
}%
\newcommand\printitwithmybluestringified{%
\myCsPrintCommand{\mybluestringified}{}{ -- }{\ensuremath}%
}%

Example with \verb|\printitwithverb|:

\printitwithverb{\lfloor}

\printitwithverb{\rfloor}

Example with \verb|\printitwithlstinline|:

\printitwithlstinline{\lfloor}

\printitwithlstinline{\rfloor}

Example with \verb|\printitwithmybluestringified|:

\printitwithmybluestringified{\lfloor}

\printitwithmybluestringified{\rfloor}

\end{document}


• If it's a single token \newcommand*\csprintcmd[1]{\texttt{\string#1} -- $#1$} is enough, no need for \verb. – Manuel Jun 25 '18 at 18:04
• @jfbu I use a group only at defining-time of the user-macros, not at the time of carrying out the user-macros. Your suggestion requires an assignment to \endlinechar and thus a local scope whenever the user-macros are used. – Ulrich Diez Jun 25 '18 at 19:36
• @Manuel As I already wrote within the answer: In my opinion the thing is sort of overkill. But it is a way of accomplishing things in case one wishes to use something for "pretty-printing" that does require a different catcode-régime, e.g., \verb or \lstinline or whatever. Of course with \verb this is somewhat pointless/redundant as \verb's \verbatim@font denotes \ttfamily as does \texttt. But there are packages that offer different/more sophisticated pretty-printing commands, e.g., with nice coloring of phrases, also requiring different catcode-régime for reading arguments. – Ulrich Diez Jun 25 '18 at 19:50
• yes your group is only for macro def (sorry for not looking too closely, but I felt the whole thing a bit overkill anyhow as you say elsewhere). I will delete my comment. As alternative to \endlinechar one can add a \relax: -- $#1$\relax} which will inhibit the trailing space (or \empty...) – user4686 Jun 25 '18 at 20:45
• for the fun, one can do this in one macro \expandafter\verb\scantokens\expandafter{\expandafter|\string#1|\relax}--$#1$, the problem solved being with extra spaces when doing \scantokens (which is like writing to a file unexpanded, hence with spaces after control words and back). If one really really is not happy enough with \string :) – user4686 Jun 25 '18 at 21:06