Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to pass some latex code to lua directly without it being parsed:

\directlua{
    PrintPrettyLatexCode("LATEX CODE GOES HERE!")
}

BUT latex tries to parse the "LATEX CODE GOES HERE!". I want it to all be treated purely as a string so that the PrintPrettyLatexCode function see's it as a string.

Slashes will need to be double slashed though. I think all that is needed is to parse the code block and make all slashes double before latex parses it(that way macros like \mymacro end up becoming \\mymacro and I think latex will then not try to expand it)

Maybe something like

\directlua{
    PrintPrettyLatexCode(\MakeDoubleSlashes{"LATEX CODE GOES HERE!"})
}

will work if \MakeDoubleSlashes exist?

share|improve this question
add comment

2 Answers

LuaTeX provides \luaescapestring for escaping characters in Lua strings:

\directlua{
  PrintPrettyLatexCode("\luaescapestring\expandafter{\detokenize{LATEX CODE GOES HERE!}}")
}

Addition

The following defines an environment PrettyPrintLatexCode:

  1. The environment contents is written to a temporary file using package filecontents.

  2. The file contents is stored in a macro by \CatchFileEdef of package catchfile.
    All characters are read verbatim by using a catcode table, where all slots are set to catcode 12 (other) and the line ends are read as byte 0x10 (\n).

  3. The data are passed via \luatexluaescapestring (that is \luaescapestring in LuaLaTeX) to the function PrettyPrintLatexCode.

The full example:

\RequirePackage{filecontents}
\RequirePackage{catchfile}
\RequirePackage{luacode}

\makeatletter
\newcommand*{\PrettyPrint@FileName}{test.file}
\newenvironment*{PrettyPrintLatexCode}{%
  \csname filecontents*\endcsname{\PrettyPrint@FileName}%
}{%
  \csname endfilecontents*\endcsname
  \CatchFileEdef{\PrettyPrint@FileData}{\PrettyPrint@FileName}{%
    \PrettyPrint@CatchSetup
  }%
  \luadirect{%
    PrettyPrintLatexCode("\luatexluaescapestring{\PrettyPrint@FileData}")%
  }%
}
\begingroup
  \lccode`\~=13 %
  \lccode`\9=10 %
\lowercase{\endgroup
  \def\PrettyPrint@CatchSetup{%
    \luatexcatcodetable\CatcodeTableOther
    \endlinechar=13 %
    \catcode13=\active
    \def~{9}%
  }%
}
\makeatother

% Provide a dummy definition for PrettyPrintLatexCode
\luadirect{%
function PrettyPrintLatexCode(str)
  texio.write_nl(
    "*****************************",
    str,
    "*****************************",
    ""
  )
end}

\begin{PrettyPrintLatexCode}
\section{Hello World}

Some text with "double" and 'single' quotes.
  and unmatched (parentheses].

Umlauts: äöüß
\end{PrettyPrintLatexCode}

\stop

Result of the dummy PrettyPrintLatexCode in the terminal/log file:

*****************************
\section{Hello World}

Some text with "double" and 'single' quotes.
  and unmatched (parentheses].

Umlauts: äöüß
*****************************
share|improve this answer
    
It should be noted that this adds spaces after every control word. –  egreg Sep 5 '12 at 21:27
    
While it does work it also adds \par on new lines(instead of using a \n). So it works but changes the code which is not good. –  AbstractDissonance Sep 5 '12 at 21:30
add comment

You could load the luacode package and use \luaexec instead of \directlua:

\luaexec{
    PrintPrettyLatexCode(\MakeDoubleSlashes{"LATEX CODE GOES HERE!"})
}

(This assumes, of course, that PrintPrettyLatexCode is a valid lua function and `MakeDoubleSlashes is a valid TeX macro.) You will still need to prefix a backslash to # and % characters; however, the ~ (tilde) and \\ (double backslash) characters needn't be escaped. To typeset a single backslash, you'll need to use \string\.

share|improve this answer
    
I do not think this will work in any way in solving my original problem? The latex code will have an issue in that stuff like "\par" will have \p interpreted as an escape code. luaexec only solves half the problem of modifying the cat codes but not fixing up lua "strings". –  AbstractDissonance Sep 5 '12 at 21:32
    
@AbstractDissonance: Thanks for clarifying your actual needs -- your initial posting didn't indicate (at least to me...) that the argument of \MakeDoubleSlashes might contain formatting-related code that needs to be parsed by LaTeX; indeed, the posting's title has something about keeping a block of code from being parsed. Please consider posting a meaningful MWE (minimum working example) of what you're trying to accomplish. –  Mico Sep 5 '12 at 21:57
    
@AbstractDissonance - Why the sarcasm (or am I misunderstanding your comment as well)? Your question has the term "stop a block of code from being parsed" in the title, and that's what I was working with. Quite evidently I didn't understand your objective -- but expressing that thought in no way implies that it was your fault I didn't understand your question. –  Mico Sep 8 '12 at 19:32
add comment

Your Answer

 
discard

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.