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.

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...

share|improve this question
    
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
add comment

1 Answer

up vote 4 down vote accepted

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}
share|improve this answer
    
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
add comment

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.