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.

If I use:

\LetLtxMacro{\MacroToUse}{\FormatText}

then using \MacroToUse invokes \FormatText exactly as desired. But, how do use this construct to invoke \FormatText*?

The MWE below yields:

enter image description here

but the desired output is the two lines being identical.

References:

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}

\usepackage{letltxmacro}


\newcommand*{\FormatColor}{}%
\NewDocumentCommand{\FormatText}{s m}{%
    \IfBooleanTF{#1}{%
        \def\FormatColor{red}%
    }{%
        \def\FormatColor{blue}%
    }%
    \textcolor{\FormatColor}{#2}%
}%

\begin{document}
\FormatText{one}
\FormatText*{two}

\newcommand{\MacroToUse}{}%

\LetLtxMacro{\MacroToUse}{\FormatText}%
\MacroToUse{one}
\LetLtxMacro{\MacroToUse}{\FormatText*}%
\MacroToUse{two}
\end{document}
share|improve this question
    
I think your questions can be used as model for all. very nice –  texenthusiast May 21 '13 at 22:46
1  
\LetLtxMacro and, in general, all patching commands should not be used with macros defined with xparse facilities. In this case \def\MacroToUse{\FormatText*} is more than sufficient. There's no reason for using \LetLtxMacro here. –  egreg May 21 '13 at 22:46
    
So you don't want to use \MacroToUse*? –  Werner May 21 '13 at 22:47
    
@Werner: Have a complex piece of code that I would prefer to not duplicate, or repackage in another macro. it currently calls the starred variant, so would be really nice if I could select to use the starred or no starred variant, and then not have to think about it later in the code. –  Peter Grill May 21 '13 at 22:51
    
You can create an auxiliary macro \newcommand*{\FormatTextStar}{\FormatText*} for which \LetLtxMacro{\MacroToUse}{\FormatTextStar} would work... –  Werner May 21 '13 at 23:02

1 Answer 1

up vote 6 down vote accepted

This is a place where \LetLtxMacro should not be used.

\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}

\usepackage{letltxmacro}


\newcommand*{\FormatColor}{}%
\NewDocumentCommand{\FormatText}{s m}{%
    \IfBooleanTF{#1}{%
        \def\FormatColor{red}%
    }{%
        \def\FormatColor{blue}%
    }%
    \textcolor{\FormatColor}{#2}%
}%

\begin{document}
\FormatText{one}
\FormatText*{two}

\newcommand{\MacroToUse}{\FormatText}
\MacroToUse{one}

\renewcommand{\MacroToUse}{\FormatText*}
\MacroToUse{two}
\end{document}

Using \let or its more powerful sibling \LetLtxMacro is basically for when you want to save the meaning of a macro in order to redefine it or, in general, when the other macro can change its meaning afterwards and you want to have its original meaning available, perhaps to restore it.

Here you never modify \FormatText, so there's no need to save its meaning.

If you're worrying about the arguments, don't. When TeX sees

\MacroToUse{two}

in the code above, it will look up the current meaning of \MacroToUse and, since it ia a macro, replace it with the replacement text. So at this stage it will be presented with

\FormatText*{two}

which is just what's needed.


In any case, you can't use

\LetLtxMacro{\foo}{\baz*}

assuming that \baz has a *-variant. The second argument to \LetLtxMacro should be a single token, and \baz* is two tokens. The * doesn't make part of the macro's name.

share|improve this answer

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.