# Printing verbatim command inputs in commands

I want to define a command \mydef that does something like this:

\mydef{\foo{bar}}


produces

\def\foo{bar} \verb|\foo=bar|


The command:

\newcommand{\mydef}[1]{\def #1}


does the first bit, but I don't seem to be able to use verbatim in a command - how do I access the contents of #1? In this context I would be happy if I could just discard the first character...

-
Welcome to tex.sx! A tip: If you indent lines by 4 spaces, then they are marked as a code sample. You can also highlight the code and click the "code" button (with "101010" on it). – lockstep Mar 26 '11 at 15:51
Could you explain the "In this context I would be happy if I could just discard the first character..." a little but more. The \ of \foo, or the b of bar? – Martin Scharrer Mar 26 '11 at 15:52
Does bar really be read verbatim or do you only want to display it so, but keep it normal for the definition of \foo? – Martin Scharrer Mar 26 '11 at 15:54

You can use \string to print the command as string and \strip@prefix in combination with \meaning to display the macro content:

\documentclass{article}

\makeatletter
\newcommand*{\mydef}[2]{%
\def#1{#2}%
{\ttfamily\string#1=\expandafter\strip@prefix\meaning#1}%
}
\makeatother

\begin{document}

\mydef\foo{bar}

\end{document}

-
That's great! Thanks a lot! – Joseph Maher Mar 26 '11 at 16:12
+1, very elegant solution. – chl Mar 26 '11 at 16:58
@chl: Thanks. The \expandafter\strip@prefix\meaning is actually taken from \@onelevel@sanitize. – Martin Scharrer Mar 26 '11 at 17:00