4

I made a randomly chosen quiz machine using LuaLaTeX and beamer for audience part that produce a pdf containning one frame per question, one slide per second to show the count down: it make a pdf with multiple redundant pages.

I'd like to produce in parallel a digest form of this quiz, in article A4 format, with only the questions and the answers. All this stuff is stored in a lua table I use once to create the beamer pdf naturally, I can reuse it to produce the digest, but as far as I know, one can produce only one pdf at the time, I'd like to store the digest in a separate digest.tex file and compile it later.

What is the command or the trick that will redirect tex.print to a separated file, or to produce two different pdf files at once if it's possible ?

Addition

Let say, I have a lua table :

t = {"Valeur app. par défaut à l'unité",69841.68, 69841 }

In beamer I use a nodes in a tikz picture to show the question, see below. But I also want something like that in a separate file (article class) :

tex.print (t[1]..":".."\\num{"..t[2].."}\\hfill"..t[3])

To have a digest of the quizz.

enter image description here

2
  • Can you provide a starter file?
    – user31729
    Commented Jul 30, 2015 at 9:35
  • It will be not minal.
    – Tarass
    Commented Jul 30, 2015 at 9:36

3 Answers 3

4

Lua has an io library that seems to work with LuaLaTeX. I don't really know what the recommended usage is though.

% !TeX program = lualatex
\documentclass{article}
\begin{document}
Foo.

\directlua{
out = io.open('somefile.tex','a')
out:write('Hello world. ')
out:write('This is different')
io.close(out)
}

\input{somefile}

\end{document}
6
  • Oh, even better :D
    – user31729
    Commented Jul 30, 2015 at 9:54
  • No need to close something at the end ?
    – Tarass
    Commented Jul 30, 2015 at 9:55
  • @Tarass I just updated, after having that same thought. Commented Jul 30, 2015 at 9:59
  • Commented lines don't work : \documentclass{article} \directlua{ out = io.open('somefile.tex','a') % out:write("\\documentclass{article}') % out:write("\\begin{document}") out:write("Hello word") % out:write("\\end{document}") io.close(out) } \end{document}
    – Tarass
    Commented Jul 30, 2015 at 10:07
  • @Tarass I don't really know what I'm doing here. You can check out the luacode package, see if that has any features that can help. If I come up with something later I'll update. Commented Jul 30, 2015 at 10:25
3

It's possible to use the standard TeX file handle routines such as \newwrite and \newread and \openout etc., letting LuaLaTeX write the code to the file as if should be written 'to' the current file (well, the TeX input processing assembly line ;-))

\documentclass{article}
\usepackage{luacode}

\newwrite\luageneratedfile

\begin{document}  
\immediate\openout\luageneratedfile=foolua.tex
\immediate\write\luageneratedfile{%
\directlua{%
  tex.print("Hello World")
  }
}
\immediate\closeout\luageneratedfile
\end{document}

This example generates foolua.tex, the only content is "Hello World" here.

5
  • Commented lines don't work : \documentclass{article} \usepackage{luacode} \newwrite\luageneratedfile \begin{document} \immediate\openout\luageneratedfile=foolua.tex \immediate\write\luageneratedfile{% \directlua{% tp = tex.print % tp("\\documentclass{article}') % tp("\\begin{document}") tp("Hello word") % tp("\\end{document}") } } \immediate\closeout\luageneratedfile \end{document}
    – Tarass
    Commented Jul 30, 2015 at 10:11
  • Well, drop the commented lines then? ;-)
    – user31729
    Commented Jul 30, 2015 at 10:12
  • I want to write to the file the latex command. If I drop them, what the use ? ;-)
    – Tarass
    Commented Jul 30, 2015 at 10:22
  • @Tarass: You did not write something about verbatim code
    – user31729
    Commented Jul 30, 2015 at 21:03
  • I'am a little confused. I never spoke about verbatim. Your code works well too except the \\ problem with directlua,but as I use a separate file for lua code, it came only beacause a test it with your example.
    – Tarass
    Commented Jul 30, 2015 at 21:29
3

Only the backslash needs to be escaped:

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{luacode}
\begin{luacode}
    function OpenFile(mode,name) out=io.open(name,mode) return end
    function CloseFile() io.close(out) return end
\end{luacode}   
\newcommand\OpenFile[2][a]{\luaexec{OpenFile('#1','#2')}}
\def\CloseFile{\luaexec{CloseFile()}}
\def\WriteLn#1{\luaexec{out:write('\string#1'.."\string\n")}}
\def\Write#1{\luaexec{out:write('\string#1')}}

\begin{document}
    Foo.

    \OpenFile[w]{somefile.tex}
    \WriteLn{Hello world from \\LaTeX. }
    \WriteLn{This is different}%
    \Write{Hello world $\\sqrt{1+x}$. }
    \Write{This is different}
    \CloseFile

    \VerbatimInput{somefile}

    \OpenFile{somefile.tex}
    \WriteLn{Hello world from \\LaTeX. }
    \WriteLn{This is different}%
    \Write{Hello world $\\sqrt{1+x}$. }
    \Write{This is different}
    \CloseFile
%   
    \VerbatimInput{somefile}

\end{document}

enter image description here

6
  • Thank you. I'd like to write not only text but latex command. I tried to repeplace This is different by by $\\sqrt{2}$. It dosen't work. Nor $\sqrt{2}$
    – Tarass
    Commented Jul 30, 2015 at 10:41
  • see my edited answer. Use package fancyvrb
    – user2478
    Commented Jul 30, 2015 at 10:56
  • It has to work with a lua table through lualatex, si my edited question.
    – Tarass
    Commented Jul 30, 2015 at 11:37
  • Ah ok, I'll see what I can do
    – user2478
    Commented Jul 30, 2015 at 12:04
  • 1
    Just for fun: I edited my answer
    – user2478
    Commented Jul 30, 2015 at 13:27

You must log in to answer this question.

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