TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

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, " ", "+")
  return str
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
 local page  = http.request( 'http://mirror-cybernet.lums.edu.pk/pub/ctan/dviware/screenview/vms/test.txt' )

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.

share|improve this question
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
up vote 7 down vote accepted

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:



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

share|improve this answer
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


to load a file from the net. Almost all file loading macros use the socket library to download (and cache) the file, if a url is specified.

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

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))

(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)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.