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.

Second question today: is there a way to pass a command as an argument of another method?

\newcommand\foo[1]{Foo does: `#1'}
\newcommand\bar[2]{Bar does: `#1{#2}'}

...

\bar{\foo}{xxx}

The output I want should be:

Bar does: `Foo does: `xxx''

Also, can someone explain to me what \expandafter does?

EDIT:

Sorry, for the question, after re-reading my code a few times, I noticed the origin of the mistake: the #1 in \bar was actually written \#1 instead. Nonetheless, thank you all for the quick answers.

share|improve this question
    
Your example should work as is, but not perhaps in the way you expect (\foo is expanded after \bar). –  Joseph Wright Apr 28 '11 at 15:17
1  
From those definitions you get exactly what you are looking for. The question is a bit too vague, though. –  egreg Apr 28 '11 at 15:21
    
(Please post one question at a time. For \expandafter I recommend taking a look at TeX-by-Topic, it's free, online, and in TeXLive.) –  Loop Space Apr 28 '11 at 18:31

2 Answers 2

The following works for me:

\documentclass{minimal}
\newcommand\Foo[1]{Foo does: `#1'}
\newcommand\BaBar[2]{BaBar does: `#1{#2}'}
\begin{document}
\BaBar{\Foo}{xxx}
\end{document}

perhaps you had an error \bar already defined? because I just copied your code and changed the name of the \bar command to avoid that error message.

share|improve this answer

In TeX there are no methods although you can define a macro in such a way as to be in many respects a method.

Consider the following minimal,

\documentclass{article}
\begin{document}
\long\def\aPieceOfCode{%
  This is a Piece of code that I need to type out in a number of ways.
  First pick the contents from the CMS and store it in macro. Then call them through your typesetter macros and hey it can even display maths \formula\ without the use of JavaScript!
}

\def\formula{$a=b^2+23 $}
\def\displayMyCode#1{#1}

\displayMyCode{\aPieceOfCode}
\end{document}

You can think of the \aPieceofCode as a function or object. In the displayMyCode macro you can pass this as an argument.

share|improve this answer
    
Just a \tiny request: Would you guys please considder using the LaTeX definitions \newcommand and \newcommand* in place of the TeX \long\def and \def to prevent redefinition existing commands. The same goes for all the \hbox and \vbox declarations I see everywhere here on Stackexchange. Using the TeX commands are very powerful in a TeXnicians hands, but can cause havoc for a newbee. –  Danie Els Apr 29 '11 at 4:51
    
@Danie Els Good suggestion, will keep it in mind. –  Yiannis Lazarides Apr 29 '11 at 10:30

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.