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 have a command that by default uses a default string and if supplied the optional parameter. I tried it using let, but that gives strange results

\documentclass{minimal}

\newcommand{\standardstring}{standard}
\newcommand{\printresult}[1][\standardstring]{%
    \let\printstring#1
    \printstring
}

\begin{document}   
\printresult

\printresult[abc]
\end{document}

This results in

standard
bc a
share|improve this question
    
Do you plan that \standardstring has a fixed value for the whole LaTeX run? –  egreg Jan 19 at 15:11
add comment

2 Answers

up vote 8 down vote accepted

In the first case, the assignment is:

\let\printstring\standardstring␣

The non-standard case:

\let\printstring abc␣

In both cases the line contains a space by the end of line. In the latter case the assignment is \let\printstring=a. Then bc and the space from the end of line is set, finally \printstring follows.

Perhaps you want something like:

\documentclass{minimal}

\newcommand{\standardstring}{standard}
\newcommand{\printresult}[1][\standardstring]{%
    \def\printstring{#1}%
    \printstring
}

\begin{document}
\printresult

\printresult[abc]
\end{document}

Result

share|improve this answer
add comment
\documentclass{minimal}
\newcommand\standardstring{standard}
\newcommand{\printresult}[1][\standardstring]{%
  \edef\standardstring{#1}#1}

\begin{document}   
\printresult

\printresult[abc]

\printresult
\end{document}
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.