0

I would like to add newlines inside a verbatim, through \directlua. Why is there no newline in the first verbatim?

\documentclass{article}
\usepackage{luacode,verbatim}
\begin{document}
\directlua{tex.print([[\unexpanded{\begin{verbatim}]]..'one\ntwo'..[[\end{verbatim}}]])}
\begin{verbatim}
one
two
\end{verbatim}
\end{document}
  • What would be the use of this? You can just type in strings: no macros or special characters will be accepted. – egreg Jan 15 '18 at 13:17
  • @egreg, The use is that the string 'one\ntwo' is actually read from a JSON file, which I wrote to with Python, which used a \n. Thanks to TeXnician's solution I now know I have to replace all occurences of '\n' in the JSON by '\r', and then it works! – Carucel Jan 15 '18 at 13:24
2

Maybe you do want to insert a carriage-return line-feed (actually carriage return is sufficient). So the following will insert one:

verbatim

\documentclass{article}
\usepackage{luacode,verbatim}
\begin{document}
\directlua{tex.print([[\unexpanded{\begin{verbatim}]]..'one\rtwo'..[[\end{verbatim}}]])}
\begin{verbatim}
one
two
\end{verbatim}
\end{document}
  • Wow, thank you very much! I have no idea why (I thought Linux didn't use the \r), but it works! – Carucel Jan 15 '18 at 13:12
  • \r seems sufficient – egreg Jan 15 '18 at 13:14
  • 1
    @Carucel You're right, Linux doesn't use it. But we're not talking about Linux, but how TeX reads output Lua passes through. So in this case \r seems necessary. – TeXnician Jan 15 '18 at 13:14
  • in general you need to insert whatever is made active by \obeylines which is character 10 (^^M) which is used as that is the usual setting of \endlinechar – David Carlisle Jan 15 '18 at 13:18
  • @DavidCarlisle Well, that was behind my understanding, but seems of course very logical. Why don't you add a more detailed answer? – TeXnician Jan 15 '18 at 13:20

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.