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}
2
  • What would be the use of this? You can just type in strings: no macros or special characters will be accepted.
    – egreg
    Commented Jan 15, 2018 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
    Commented Jan 15, 2018 at 13:24

1 Answer 1

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}
5
  • Wow, thank you very much! I have no idea why (I thought Linux didn't use the \r), but it works!
    – Carucel
    Commented Jan 15, 2018 at 13:12
  • \r seems sufficient
    – egreg
    Commented Jan 15, 2018 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
    Commented Jan 15, 2018 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 Commented Jan 15, 2018 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
    Commented Jan 15, 2018 at 13:20

You must log in to answer this question.

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