# How to write # in pdf with pdflatex and lualatex?

For escaping / in pdf I must use #2fx, but I do not know how to write # characters in pdf. When I write #, I obviously get an error. When I use \#, it exactly writes in pdf, and when I use \tl_to_str:n{#}, I have # but I need exactly # character in pdf. Now you will see my example using \#, but in the pdf I have \# instead of #. Thanks everybody for the help.

\documentclass{article}
\usepackage{l3pdf}
\begin{document}
test
\ExplSyntaxOn
\pdf_object_new:nn{obj}{dict}
\pdf_object_write:nx{obj}{/Type\c_space_tl/test#2fx-test}
\ExplSyntaxOff
\end{document}

• I'm not sure if this is the proper way to fix this, but try \cs_generate_variant:Nn \pdf_object_write:nn { ne } and then \pdf_object_write:ne{obj}{/Type\c_space_tl/test#2fx-test}. Also, if you're using :nx just because of \c_space_tl, you can replace it by ~, then you don't need any expansion: \pdf_object_write:nn{obj}{/Type~/test#2fx-test}. – Phelype Oleinik Oct 25 '19 at 21:16
• @Phelype Oleinik Ok,but how in this case i can use # character as number of argument,if i will need in it? – Aleksandr Kozlovskiy Oct 25 '19 at 21:35
• But in that example you are not inside a macro, so #2 is not an argument. If you're using \pdf_object_write:nn from inside a macro, then #2 should be replaced by the second argument. Can you please post an example code for this case? – Phelype Oleinik Oct 25 '19 at 21:38
• Sounds as if you actually want to use one of the l3str-convert macros, but I can't test now. – Ulrike Fischer Oct 25 '19 at 21:45
• @Ulrike Fischer How i can use this macros for # character? Can you please write an example and i will test it myself. – Aleksandr Kozlovskiy Oct 25 '19 at 22:01

You seem to want what in pdflatex is provided by the \pdfescapename command. The pdftexcmds package provides luatex emulation. With expl3 you can use the commands of the l3str-convert module.

\documentclass{article}
\usepackage{l3pdf}
\ExplSyntaxOn
\pdf_uncompress:
\ExplSyntaxOff
\begin{document}
test
\ExplSyntaxOn
\pdf_object_new:nn{obj}{dict}
\str_set_convert:Nnnn\l_tmpa_str {test/slash}{}{utf8/name}
\pdf_object_write:nx{obj}{/Type\c_space_tl /\l_tmpa_str}
\ExplSyntaxOff
\end{document}


which gives

 << /Type /test#2Fslash >>


If you know what you want to produce, you can insert the # e.g. like this:

\pdf_object_write:nx
{obj}
{/Type\c_space_tl /test\char_generate:nn{35}{12}2Fslash}


or

\pdf_object_write:nx
{obj}
{/Type\c_space_tl /test\token_to_str:N#2Fslash}