TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I realize that the title might be not understandable. I want to have a command \printit{} which takes 1 arguments and display it after some test. Inside \printit I would like to test if #1 is just a piece of text, or if it is itself a macro. For simplicity, I could assume the following cases :

  • #1 is just some text, like hallo
  • #1 has the form \mycomA{content}
  • #1 has the form \mycomB{content}

where \mycomA{} and \mycomB{} are commands defined by myself. Depending on the case, I want \printit{} to typeset hallo, or content with some formatting depending on the case. For simplicity I could also assume that the arguments of \mycomA and \mycomB are simple text.

How should I proceed ? Here is the empty MWE :)

\newcommand{\mycomA}[1]{\textbf{#1}}% just for the example
\newcommand{\mycomB}[1]{\textit{#1}}% just for the example
% If #1 has the form mycomA(...) THEN 
 case A : content = % extract the content of mycomA
% ElseIf #1 has the form mycomB(...) THEN 
 case B : content = % extract the content of mycomB
% Else THEN 
 case C : content = #1 


share|improve this question
It is not clear to me why not to write \newcommand{\printit}[1]{#1}. The special formatting is in the definition of \mycomA and \mycomB, respectively, isn't it? Please elaborate what exactly you wish to achieve. – Stephan Lehmke Mar 28 '12 at 13:42
no, the formatting is not in the definition of mycomA. For example, say, if the argument of \printit is \mycomA, then print it in red. – nicolas roy Mar 28 '12 at 14:02

If the commands possibly appearing in the argument are of the form \mycomA or similar, that is, commands that take an argument, you can prepare a list of them:

  % other similar commands

This works because after the group the commands will regain their former definition and \@firstofone simply strips off the braces from the argument.

If there's less control on the macros that can appear a more complicated approach must be taken.


Let me do some guesswork. You want to have \printit treat differently the argument passed to it with respect to the "outer" definition of \mycomA and \mycomB. Let's say that it has to change \mycomA into "print in red" and return "status A", and \mycomB to print its argument in green and return "status B":

  % possibly other similar definitions
\newcommand{\printit}[1]{\gdef\status{0}% in case no command appears
share|improve this answer
no, the replacement text of my \mycomA is more complicated. But why not using the same trick, by redefining, in roy@disable, \mycomA by something like \let\mycomA{\gdef\case{A}\gdef\content{\@firstofone}} , in order to store the content in \content and the information about which command was used in \case ? – nicolas roy Mar 28 '12 at 14:00
@nicolasroy Of course you're right: what must be disabled are just the "wrapper" macros. Please, make clearer whether your aim is not simply to print the argument. – egreg Mar 28 '12 at 14:02
ok. But how can i store \case{A} ? I can't put the \@firstofone in braces ... – nicolas roy Mar 28 '12 at 14:10
You can redefine \mycomA to be whatever you like. It's hard to say more without knowing more of what you want to do. Please, state clearly your problem. – egreg Mar 28 '12 at 14:12
@nicolasroy The correct syntax is \renewcommand\mycomA[1]{\gdef\case{A}\gdef\content{##1}}. But as @egreg says, it is incomprehensible what your're getting at with this. – Stephan Lehmke Mar 28 '12 at 15:41

I would just redefine \mycomA and \mycomB, respectively, to the way they should be printed by \printit:

share|improve this answer

Your Answer


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.