During pdfLaTeX processing, I'm writing a string of key-value pairs to an external file. Sometimes the value will contain \# and I want only the # to be written to the file.

Here is my code which works fine as long as I just need to write plain text. Assume that every key is defined as \@empty by default; it is only written to the file if it has a value other than \@empty.

e.g. \def\objects{\@empty}\def\mystring{\@empty}

  \edef\tmpval{\csname #1\endcsname}

Then later this line writes the key-value pairs to the external file:


I suppose I need to loop through \tmpval checking for \char23? Is that a step in the right direction?

Edit: An example of the LaTeX input:

\mytag[mystring=val, objects=App\#1 App\#2,caption={my title}]{myarg}

I parse and retrieve the key-value pairs and I want to write this line to the external file:

mystring=val, objects=App#1 App#2

2 Answers 2


You can simply locally redefine \#, which is a macro (a control character to be specific, normal macros are control words) to expand to a verbatim #. This can be simply done using the newverbs package which provides \Verbdef.

% Preamble

% write code

Without this package you can do it the following way:

% outside of any macro
% globally defines a macro \hashchar which holds a verbatim `#`

% write code

Usually people would use \@hashchar (requires \makeatletter .. \makeatother outside package or class files) to reduce the risk of name clashes.

(Not tested due to the lack of a MWE.)


I would defer the execution of the macro when # has category code 12:

\newcommand{\getkeyval}{\begingroup\catcode`#=12 \@getkeyval}
  \edef\tmpval{\csname #1\endcsname}%

You should not write \# in the argument, but only #. Seeing a "real life" example would be useful.

You must log in to answer this question.

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