8

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
  • Do you plan that \standardstring has a fixed value for the whole LaTeX run? – egreg Jan 19 '14 at 15:11
8

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

1
\documentclass{minimal}
\newcommand\standardstring{standard}
\newcommand{\printresult}[1][\standardstring]{%
  \edef\standardstring{#1}#1}

\begin{document}   
\printresult

\printresult[abc]

\printresult
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.