3

I can’t find a way to print the content of a LaTeX3 string and/or a text verbatim (or to be more precise, I found a solution by writing the content to a file and then displaying the verbatim content of that file but it looks really ugly):

enter image description here

\documentclass{article}
\ExplSyntaxOn

\NewDocumentCommand{\printMeVerbatim}{m}{ %% Don’t know what to put here: I managed to write it in a file to write its content verbatim, %% but it sounds really dirty #1 }

\ExplSyntaxOff

\begin{document}

I would like to print this string verbatim: ``\printMeVerbatim{Hello $\delta$.}'' like: \begin{verbatim} Hello $\delta$. \end{verbatim} ideally using LaTeX3 syntax.

\end{document}

tobiasBora
  • 8,684

1 Answers1

7

enter image description here

v is provided by ltcmd for this sort of use.

\documentclass{article}
\ExplSyntaxOn

\NewDocumentCommand{\printMeVerbatim}{v}{%%%%% %% Don’t know what to put here: I managed to write it in a file to write its content verbatim, %% but it sounds really dirty \begin{flushleft}\ttfamily% #1%%%%% \end{flushleft} }

\ExplSyntaxOff

\begin{document}

I would like to print this string verbatim: ``\printMeVerbatim{Hello $\delta$.}'' like: \begin{verbatim} Hello $\delta$. \end{verbatim} ideally using LaTeX3 syntax.

\end{document}

David Carlisle
  • 757,742
  • I did it as a display like verbatim as requested so the quotes are weirdly placed. You coulddo it inline as \texttt{#1} instead. – David Carlisle Aug 30 '23 at 09:51
  • Thanks, but in my actual problem, the string comes from a map: \seq_map_inline:Nn \l_robExt_placeholders_seq {- ~ Placeholder ~ called ~ ``##1'' contains:} where ##1 can contain for instance underscores that gets interpreted by latex. Any idea how to proceed in that case? – tobiasBora Aug 30 '23 at 09:51
  • 3
    @tobiasBora apart from the fact it's almost completely different to your question:-) just use \detokenize{#1} (or \tl_to_str:n which is same thing) – David Carlisle Aug 30 '23 at 09:53
  • Awesome, thanks! (I can only accept it in 5mn) – tobiasBora Aug 30 '23 at 09:54
  • I don’t know why, but sometimes my newlines are turned into \Omega when printed. I created a new question here https://tex.stackexchange.com/questions/694731/why-are-my-newlines-turned-into-omega – tobiasBora Aug 30 '23 at 13:08
  • @tobiasBora ah that's a feature... https://github.com/latex3/latex2e/pull/1087 – David Carlisle Aug 30 '23 at 13:35
  • Hum wait… I always want +v to parse newline as a newline (it might contain non-LaTeX code, so parsing newline as \par would break all my code!)… I only want to display it nicely, so I guess I need to replace ^^J with newlines before printing the string? – tobiasBora Aug 30 '23 at 14:24
  • @tobiasBora but verbatim (for example) makes each end of line \par what would you mean by a "newline"? a character 10, as you see, is just a character not a linebreak. – David Carlisle Aug 30 '23 at 14:31
  • I want the string to contain the utf-8 code for what people usually denote as \n in other programming languages, as it might contain non-latex document (see my usecase in https://github.com/latex3/latex2e/pull/1087#issuecomment-1699332910). But I also want to print this string in the pdf, and in that case, when the string contains \n, I want to print a latex new line, I guess using \\. – tobiasBora Aug 30 '23 at 14:57
  • that isn't the way latex or pdf work, a character 10 will not print as a newline – David Carlisle Aug 30 '23 at 15:00
  • Ok thanks, but I tried to do \str_replace_all:Nnn \l_robExt_tmp_str {^^J} { \newline }, now I literally see the text \newline in my pdf… What have I done wrong? – tobiasBora Aug 30 '23 at 15:17
  • _str variables are like \string they just have catcode 12 characters no command names. – David Carlisle Aug 30 '23 at 15:18
  • Ok, so using tl it works better. I also need to replace spaces: \tl_set_eq:Nc \l_robExt_tmp_str { l_robExt_placeholder_#2_str } \tl_replace_all:Nnn \l_robExt_tmp_str {^^J} { \par } \tl_replace_all:Nnn \l_robExt_tmp_str { ~ } { \ } seems to work fine for now. – tobiasBora Aug 30 '23 at 18:45
  • Ok thanks, so using tl it seems to work. I also added a special case for spaces (hopefully this is enough for all cases), details in https://tex.stackexchange.com/a/694776/116348 – tobiasBora Aug 30 '23 at 18:50