3

I have a lua function to withch I want to pass some extra optional latex code (basycally a macro with a parameter, macro can change, parameter too). I have to double backslash the name of the macro, I understand why, but I hope there is a tricky way to avoid this "unnatural" double backslash (latex view side speaking) ?

enter image description here

\documentclass[french]{article}
\usepackage{polyglossia,xcolor}
\directlua{require("bob.lua")}


\def\Red{red}
\newcommand{\MyC}[1]{\colorbox{\Red}{#1}}

\newcommand{\TP}[1][]{%
    \directlua{Semaine("\unexpanded{#1}")}
    }

\begin{document}

% How to avoid this following double \\ ?
% if possible ...

\TP
\TP[\\MyC{works}]
\TP{\MyC{doesn't'}}
\end{document}

bob.lua file :

t = {}
sem = 1
t[1] = "31 Août" -- the Semaine function calculates this values
t[2] = "5 Sept"
t[3] = "A"

function Semaine (option)
tex.print ("\\rule[.5em]{0pt}{1em}\\colorbox{gray!30!white}{"
..sem.." -- "
..t[3].."}\\quad"..option.."\\hfill\\textit{"
..t[1].." -- "..t[2].."}\\par")
end
  • You could write \TP{\MyC{ça marche !!} instead of \TP[\\MyC{ça marche !!}]... – Mico Aug 6 '15 at 14:48
  • Works in my to simple example. See my update. – Tarass Aug 6 '15 at 15:07
1

The problem is

\directlua{Semaine("\unexpanded{#1}")}

At least it prevents, that #1 is expanded too early. But #1 is user contributed content, which might contain anything, including quotes, which would break the intended Lua syntax. Therefore LuaTeX provides \luaescapestring, which can be used as \luatexluaescapestring in LuaLaTeX. It automatically quotes the special characters to get a safe Lua string:

\directlua(Semaine("\luatexluaescapestring{\unexpanded{#1}}")}%

Then \MyC is kept as macro and \\ remains the command for a new line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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