# LuaLaTeX, sprint verbatim text

The code below, uses the lua, socket.http library to pull a web page (for simplicity I have used a link to a text file). I have used tex.sprint to pass it back to the typesetting engine. However I would have preferred to print within a verbatim environment or preferably using the listings environment. Any suggestions as to how I can achieve this?

\documentclass{article}
\usepackage[utf8]{luainputenc}
\begin{document}
\bgroup
\catcode\%=12
\catcode\_=12
\catcode\^=12
\catcode\&=12
\directlua{

local http = require("socket.http")
function url_encode(str)
if (str) then
str = string.gsub (str, "\string\n", "\string\r\string\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
function url_decode(str)
str = string.gsub (str, "+", " ")
str = string.gsub (str, "%%(%x%x)",
function(h) return string.char(tonumber(h,16)) end)
str = string.gsub (str, "\string\n", "\string\n")
return str
end
local page  = http.request( 'http://mirror-cybernet.lums.edu.pk/pub/ctan/dviware/screenview/vms/test.txt' )
tex.sprint(page)

}
\egroup
\end{document}


Note, the MWE contains two functions that I haven't used, for encoding and decoding the url. I left them in case you want to try a different url. As a sideline I find it amazing that old 'TeX' in new clothes can talk to the web.

-
I put in a lua language hint for the syntax highlighter. This split your code block into three, but it still seems to copy and paste fine. I won't be at all offended if you want to roll it back! – qubyte Feb 23 '12 at 4:51
@MarkS.Everitt Never thought of it, thanks. – Yiannis Lazarides Feb 23 '12 at 5:06

There are some problems with your code:

1. don't use \usepackage[utf8]{luainputenc} in your document as utf8 is the default
2. Put the Lua code in a separate file. If that is not feasible, use the luacode* environment from the luacode package See the question about luacode enviroments. Then you don't need to mess with the cat codes.
3. The two functions you define are not used (and are unnecessary).

Here is a solution which works with the listings environment:

\documentclass{article}
\usepackage{luacode}
\usepackage{listings}

\begin{document}

\begin{luacode*}
local http = require("socket.http")
local page  = http.request( 'http://mirror-cybernet.lums.edu.pk/pub/ctan/dviware/screenview/vms/test.txt' )

tex.print("\\begin{lstlisting}")
tex.print(page:gsub("\n","\r"))
tex.print("\\end{lstlisting}")
\end{luacode*}
\end{document}

-
Thanks, for your answer, just finding my way through Lua:) – Yiannis Lazarides Feb 23 '12 at 9:05
@YiannisLazarides I have to admit that it took me a few attempts to get the above code right. I first tried with verbatim (from the verbatim package) and then with lstlistings. Finally I chose to do the gsub above and all the problems went away. I strongly suggest to separate Lua and TeX code by putting the Lua code in some extra file. – topskip Feb 23 '12 at 9:08
Thanks for the suggestion to separate the Lua code in another file, it is more tidy as well. – Yiannis Lazarides Feb 23 '12 at 9:13
@PatrickGundlach: Any idea why substituting \n to \r is not needed in ConTeXt? – Aditya Feb 23 '12 at 16:44
@Aditya no idea. I guess its one of the magic things ConTeXt has built in. – topskip Feb 23 '12 at 18:35

FWIW, in ConTeXt you can simply use

\starttext
\typefile{http://mirror-cybernet.lums.edu.pk/pub/ctan/dviware/screenview/vms/test.txt}
\stoptext


If you want to do this manually, the following works:

\starttext
\startluacode
local http = require("socket.http")
local page  = http.request( 'http://mirror-cybernet.lums.edu.pk/pub/ctan/dviware/screenview/vms/test.txt' )
tex.print(string.format("\\starttyping %s \\stoptyping", page))
\stopluacode
\stoptext


(I could not get the similar code to work in LuaLaTeX because the luacode environment in LaTeX uses different catcodes than the luacode environment in ConTeXt)

-