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 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 :)

\documentclass{article}
\newcommand{\mycomA}[1]{\textbf{#1}}% just for the example
\newcommand{\mycomB}[1]{\textit{#1}}% just for the example
\newcommand{\printit}[1]{%
% 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 

\begin{document}
    \printit{hallo}
    \printit{\mycomA{123}}
    \printit{\mycomB{qweqwe}}
    \printit{hallo}

\end{document}
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
add comment

2 Answers 2

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:

\makeatletter
\newcommand{\roy@disable}{%
  \let\mycomA\@firstofone
  \let\mycomB\@firstofone
  % other similar commands
}
\newcommand{\printit}[1]{%
  \begingroup\roy@disable
  #1
  \endgroup
}
\makeatother

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.

Edit

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":

\makeatletter
\newcommand{\roy@change}{%
  \def\mycomA##1{\gdef\roy@status{A}\textcolor{red}{##1}}
  \def\mycomB##1{\gdef\roy@status{B}\textcolor{green}{##1}}
  % possibly other similar definitions
}
\newcommand{\printit}[1]{\gdef\status{0}% in case no command appears
  \begingroup
  \roy@change
  #1%
  \endgroup
}
\makeatother
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
1  
@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
show 3 more comments

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

\newcommand\printit[1]{%
  \begingroup
     \renewcommand\mycomA[1]{\textcolor{red}{##1}}%
     \renewcommand\mycomB[1]{\textcolor{blue}{##1}}%
     #1%
  \endgroup
}%
share|improve this answer
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.