4

I have a macro which uses \typeout to print messages. I would like to unescape these.

\newcommand{\mymessage}[1]{
  \typeout{#1}
}

Then \mymessage{foo\#bar} should print foo#bar. Right now it prints foo\#bar.

Edit:

Answers below show how to solve the specific problem of unescaping \# character. However, this was meant as an example only. I also need to unescape \% and any other such combination.

2
  • “Any other” is a bit too generic; can you make a list? Anyway, you asked for \#.
    – egreg
    Nov 7, 2015 at 10:40
  • What I really want to do is to typeout url's which are passed to a latex command, such that they are without any escapes.
    – highsciguy
    Nov 7, 2015 at 10:50

2 Answers 2

3

Edit to follow up on OP's modifications.

Perhaps this will be ok with your yet to be precisely specified yet unknown constraints:

\documentclass{article}
\newcommand{\mymessage}[1]{\bgroup\escapechar-1 \typeout{\detokenize{#1}}\egroup}

\begin{document}

\mymessage{foo\#bar\!foo\%hello\&world}
\end{document}

produces:

foo#bar!foo%hello&world

original answer

If you are not using \mymessage from inside macros, you could try

\documentclass{article}
\newcommand{\mymessage}{\bgroup\lccode`~=`\\\lowercase{\let~\string}%
                        \catcode92 13 \mynoescapemessage}
\newcommand{\mynoescapemessage}[1]{\typeout{#1}\egroup}

\begin{document}

\mymessage{foo\#bar\!foo\hello\world}
\end{document}

Produces:

foo#bar!foohelloworld

Notice that the reason the thing is a bit complicated (I could have chosen catcode 9 (aka "ignore")) is to avoid:

foo##bar

due to special treatment by TeX of # tokens.

0

Redefine \# (locally):

\newcommand{\mymessage}[1]{%
  \begingroup\edef\#{\string##}%
  \typeout{#1}%
  \endgroup
}

\mymessage{foo\#bar}

The output is

foo#bar

Be careful with protecting end-of-lines in your macro definitions.

Here is how you can add escape codes:

\makeatletter
\newcommand{\mymessage}[1]{%
  \begingroup\mymessage@escapes
  \typeout{#1}%
  \endgroup
}

\newcommand{\mymessage@escapes}{%
  \edef\#{\string##}%
  \let\%\@percentchar % already provided by LaTeX
  \edef\{{\string{}\edef\}{\string}}%
  \edef\${\string$}%
}

\mymessage{\{foo\#{bar}\%\$\}}

Also note that balanced braces need not be escaped in a \typeout.

The output is

{foo#{bar}%$}
1
  • Please see my edit.
    – highsciguy
    Nov 7, 2015 at 10:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .