Debian Codegraph @

texmf-dist/source/lualatex/luacode/luacode.dtx

raw
1 % \iffalse meta-comment (by the way, this file is contains UTF-8)
2 %
3 % Written in 2010, 2011 by Manuel Pégourié-Gonnard <[email protected]>
4 %
5 % This work may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either version 1.3
7 % of this license or (at your option) any later version.
8 % The latest version of this license is in
9 % http://www.latex-project.org/lppl.txt
10 % and version 1.3 or later is part of all distributions of LaTeX
11 % version 2005/12/01 or later.
12 %
13 % This work has the LPPL maintenance status `maintained'.
14 %
15 % The Current Maintainer of this work is Manuel Pégourié-Gonnard
16 %
17 % This work consists of the main source file luacode.dtx
18 % and the derived files
19 % luacode.sty luacode.pdf
20 %
21 % Unpacking:
22 % tex luacode.dtx
23 % Documentation:
24 % pdflatex luacode.dtx
25 %
26 %<*ignore>
27 \begingroup
28 \def\x{LaTeX2e}%
29 \expandafter\endgroup
30 \ifcase 0\ifx\install y1\fi\expandafter
31 \ifx\csname processbatchFile\endcsname\relax\else1\fi
32 \ifx\fmtname\x\else 1\fi\relax
33 \else\csname fi\endcsname
34 %</ignore>
35 %<*install>
36 \input docstrip.tex
37
38 \keepsilent
39 \askforoverwritefalse
40
41 \preamble
42
43 See the source file for author and licensing information.
44
45 \endpreamble
46
47 \generate{%
48 \usedir{tex/lualatex/luacode}%
49 \file{luacode.sty}{\from{luacode.dtx}{texpackage}}%
50 }
51
52 \generate{%
53 \usedir{doc/lualatex/luacode}%
54 \file{test-luacode.tex}{\from{luacode.dtx}{testlatex}}%
55 }
56
57 \obeyspaces
58 \Msg{************************************************************************}
59 \Msg{*}
60 \Msg{* To finish the installation you have to move the following}
61 \Msg{* files into a directory searched by TeX:}
62 \Msg{*}
63 \Msg{* luacode.sty}
64 \Msg{*}
65 \Msg{* Happy TeXing!}
66 \Msg{*}
67 \Msg{************************************************************************}
68
69 \endbatchfile
70 %</install>
71 %<*ignore>
72 \fi
73 %</ignore>
74 %<*driver>
75 \documentclass{ltxdoc}
76 \usepackage[utf8]{inputenc}
77 \usepackage[T1]{fontenc}
78 \usepackage{textcomp}
79 \usepackage{lmodern}
80 \usepackage{fixltx2e, mparhack}
81
82 \usepackage{xcolor}
83 \usepackage{booktabs}
84 \usepackage{xspace}
85 \usepackage[a4paper]{geometry}
86 \usepackage[english]{babel}
87 \usepackage[colorlinks]{hyperref}
88 \usepackage{bookmark}
89
90 \newcommand*\email [1] {<\href{mailto:#1}{#1}>}
91 \newcommand \file {\nolinkurl}
92 \newcommand \pk {\textsf}
93 \newcommand \cmdname {\texttt}
94
95 \pdfstringdefDisableCommands{%
96 \def\cs#1{\@backslashchar #1}}
97
98 \MakeShortVerb\|
99 \renewcommand\MacroFont{\ttfamily\small\color{blue}}
100
101 \begin{document}
102 \DocInput{luacode.dtx}%
103 \end{document}
104 %</driver>
105 % \fi
106 %
107 % \CheckSum{0}
108 %
109 % \CharacterTable
110 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
111 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
112 % Digits \0\1\2\3\4\5\6\7\8\9
113 % Exclamation \! Double quote \" Hash (number) \#
114 % Dollar \$ Percent \% Ampersand \&
115 % Acute accent \' Left paren \( Right paren \)
116 % Asterisk \* Plus \+ Comma \,
117 % Minus \- Point \. Solidus \/
118 % Colon \: Semicolon \; Less than \<
119 % Equals \= Greater than \> Question mark \?
120 % Commercial at \@ Left bracket \[ Backslash \\
121 % Right bracket \] Circumflex \^ Underscore \_
122 % Grave accent \` Left brace \{ Vertical bar \|
123 % Right brace \} Tilde \~}
124 %
125 % \title{The \pk{luacode} package}
126 % \author{Manuel P\'egouri\'e-Gonnard \email{[email protected]}}
127 % \date{v1.2a 2012/01/23}
128 %
129 % \maketitle
130 %
131 % \begin{abstract}
132 % Executing Lua code from within \TeX\ with |\directlua| can sometimes be
133 % tricky: there is no easy way to use the percent character, counting
134 % backslashes may be hard, and Lua comments don't work the way you expect.
135 % This package provides the |\luaexec| command and the |luacode(*)|
136 % environments to help with these problems, as well as helper macros and a
137 % debugging mode.
138 % \end{abstract}
139 %
140 % \tableofcontents
141 %
142 % \section{Documentation}
143 %
144 % \subsection{Lua code in \LaTeX}
145 %
146 % For an introduction to the most important gotchas of |\directlua|, see
147 % \file{lualatex-doc.pdf}. Before presenting the tools in this package, let me
148 % insist that the best way to manage a non-trivial piece of Lua code is
149 % probably to use an external file and source it from Lua, as explained in the
150 % cited document.
151 %
152 % \medskip
153 %
154 % \DescribeMacro\luadirect
155 % First, the exact syntax of |\directlua| has changed along version of
156 % Lua\TeX, so this package provides a |\luadirect| command which is an exact
157 % synonym of |\directlua| except that it doesn't have the funny, changing
158 % parts of its syntax, and benefits from the debugging facilities described
159 % below (\ref{dbg}).\footnote{And expands in two steps instead of one. If
160 % you don't know what it means, then you hopefully don't need to.}
161 %
162 % The problems with |\directlua| (or |\luadirect|) are mainly with \TeX\
163 % special characters. Actually, things are not that bad, since most special
164 % characters do work, namely: |_|, |^|, |&|, \verb+$+, |{|, |}|. Three are a
165 % bit tricky but they can be managed with |\string|: |\|, |#| and |~|. Only
166 % |%| is really hard to obtain. Also, \TeX\ macros are expanded, which is good
167 % since it allows to pass information from \TeX\ to Lua, but you must be
168 % careful and use only purely expandable macros.
169 %
170 % \DescribeMacro\luaexec
171 % The |\luaexec| command is similar to |\luadirect| but with a few additional
172 % features:\footnote{And one major drawback: it is not purely expandable. See
173 % previous note.} |\\| gives a double backslash (see note below)
174 % |\%| a percent character, and |~| just works. For single backslashes,
175 % |\string| is still needed. Also, \TeX\ macros are expanded.
176 %
177 % \DescribeEnv{luacode}
178 % The |luacode| environment is similar to |\luaexec|, except that you can now
179 % use |%| and |#| directly (but |\%| and |\#| also work) and the line breaks
180 % are respected, so that you can use line-wise Lua comments in the normal way,
181 % without mistakenly commenting the rest of the chunk.
182 %
183 % Only the backslash and the braces keep their special meaning, so that macros
184 % still work as usual, and you still need to use |\string| to get a single
185 % backslash.
186 %
187 % \DescribeEnv{luacode*}
188 % The variant |luacode*| goes further and makes even backslash a normal
189 % character, so that you don't need any trick to obtain a single backslash. On
190 % the other end, macros don't work any more. So, the content of a |luacode*|
191 % is interpreted exactly as if it were in a normal Lua file, directly fed to
192 % the Lua interpreter without any \TeX\ intervention.
193 %
194 % \medskip
195 %
196 % The following table summarizes how to use special characters with the
197 % various commands and environments.
198 %
199 % \begin{center}
200 % \begin{tabular}{rcccc}
201 % \toprule
202 % & |\luadirect| & |\luaexec| & |luacode| & |luacode*| \\
203 % \midrule
204 % Macros & Yes & Yes & Yes & No \\
205 % Single backslash & |\string\| & |\string\| & |\string\| & Just |\| \\
206 % Double backslash & |\string\\| & |\\| & |\\| & |\\| \\
207 % Tilde & |\string~| & |~| & |~| & |~| \\
208 % Sharp & |\string#| & |\#| & |#| (or |\#|) & |#| \\
209 % Percent & No easy way & |\%| & |%| (or |\%|) & |%| \\
210 % \TeX\ comments & Yes & Yes & No & No \\
211 % Lua line comments & No & No & Yes & Yes \\
212 % \bottomrule
213 % \end{tabular}
214 % \end{center}
215 %
216 % \noindent\textbf{Backslashes and Lua strings.}
217 % In the table and descriptions above, ``double backslash'' means that the Lua
218 % interpreter will see a double backslash. It may then turn it into a single
219 % backslash in the context of a Lua string delimited by single or double quotes
220 % as opposed to a Lua string delimited by brackets, see
221 % \href{http://www.lua.org/pil/2.4.html}{\emph{Programming in Lua} section
222 % 2.4}. Similarly, a single backslash may or may not be interpreted as
223 % starting an escape sequence. For example:
224 % \begin{verbatim}
225 % \begin{luacode}
226 % a = "\\" -- a contains a single backslash
227 % b = [[\\]] -- b contains two backslashes
228 % c = "\\\\" -- c contains two backslashes too
229 % d = "line one\nline two" -- d contains a newline character
230 % e = [[single\nline]] -- e contains no newline character
231 % \end{luacode}
232 % \end{verbatim}
233 % The alert reader may notice that in the case of |\luadirect| and |\luaexec|,
234 % single backslashes are a bit weird. For example with
235 % \begin{verbatim}
236 % \luaexec{texio.write_nl("line one\string\nline two")}
237 % \end{verbatim}
238 % \TeX\ will see |\nline| as a control sequence which is the ``argument'' of
239 % |\string| and the Lua interpreter will consider only |\n| as an escape
240 % sequence, and |line| as independent characters. In practice, this should not
241 % have any unwanted consequences (except perhaps on the sanity of the reader).
242 %
243 % \medskip
244 %
245 % \noindent\textbf{Technical notes on environments.}
246 % \DescribeEnv{luacodestar}
247 % The environments will not work inside the argument of a command (just as
248 % with verbatim commands and environments). Also, you are supposed to leave a
249 % space (or end-of-line) after the |\begin{luacode}| or |\begin{luacode*}|,
250 % which is probably a natural thing to do anyway. Finally, if you wish to
251 % define derived environments, you'll need to use |\luacode| \dots
252 % |\endluacode| instead of the usual |\begin| |\end| pair in your
253 % environment's definition. For the stared variant, use |\luacodestar| and
254 % |\endluacodestar|.
255 %
256 % \medskip
257 %
258 % The test file (section~\ref{test}, or \file{test-luacode.tex} in the same
259 % directory as this document) provides stupid but complete examples.
260 %
261 % \subsection{Helper macros}
262 %
263 % As mentioned in the previous section, except for trivial pieces of codes (or
264 % examples) it is good practice to keep all your Lua code in separate |.lua|
265 % files and then use |\luadirect| only to |require()| or |dofile()| it and
266 % define \LaTeX\ wrappers for some functions, eg:
267 % \begin{quote}
268 % |\newcommand*\foo[2]{\luadirect{foo("#1", #2)}}|
269 % \end{quote}
270 % This way, problems with \TeX\ special characters are avoided, since most of
271 % the Lua is never seen by \TeX. Unfortunately, there is still potential
272 % for problems. For example |\foo{a"b}{2}| will cause the Lua interpreter to
273 % complain since the |"| in |#1| will end the string; we want the Lua
274 % interpreter to see |"a\"b"| as the first argument.
275 %
276 % \DescribeMacro\luastring
277 % Fortunately, Lua\TeX\ offers a primitive that does exactly what we need:
278 % escape characters that need to be escaped in a Lua string. Unfortunately, it
279 % has a very long name (especially in the prefixed form available in \LaTeX):
280 % |\luatexluaescapestring|. Also, you need to think to use quotes in addition
281 % to this primitive. So this package provides a shorter version: |\luastring|
282 % that also include the quotes, so a safer version of |\foo| might be defined
283 % as
284 % \begin{quote}
285 % |\newcommand*\foo[2]{\luadirect{foo(\luastring{#1}, #2)}}|
286 % \end{quote}
287 %
288 % \DescribeMacro\luastringN
289 % \DescribeMacro\luastringO
290 % It should be noted that the argument of |\luastring| is fully
291 % expanded\footnote{If you don't know what this means, just skip this
292 % paragraph.} before being turned into a Lua string. In case where such an
293 % expansion is unwanted, two variants are provided: |\luastringN| for no
294 % expansion, and |\luastringO| for one-level expansion (of the first token)
295 % only.
296 %
297 % \subsection{Debugging} \label{dbg}
298 %
299 % \DescribeMacro\LuaCodeDebugOn
300 % \DescribeMacro\LuaCodeDebugOff
301 % The commands |\luadirect| and |\luaexec| as well as the environments
302 % |luacode| and |luacode*| can optionally print the Lua code as it will be
303 % seen by the Lua interpreter in the log file before executing it. The feature
304 % is disabled by default and can be turned on and off using |\LuaCodeDebugOn|
305 % and |\LuaCodeDebugOff| (which obey the usual \TeX\ scoping rules).
306 %
307 % \section{Implementation}
308 %
309 % \begin{macrocode}
310 %<*texpackage>
311 % \end{macrocode}
312 %
313 % \subsection{Preliminaries}
314 %
315 % Catcode defenses.
316 %
317 % \begin{macrocode}
318 \begingroup\catcode61\catcode48\catcode32=10\relax% = and space
319 \catcode123 1 % {
320 \catcode125 2 % }
321 \catcode 35 6 % #
322 \toks0{\endlinechar\the\endlinechar}%
323 \edef\x{\endlinechar13}%
324 \def\y#1 #2 {%
325 \toks0\expandafter{\the\toks0 \catcode#1 \the\catcode#1}%
326 \edef\x{\x \catcode#1 #2}}%
327 \y 13 5 % carriage return
328 \y 61 12 % =
329 \y 32 10 % space
330 \y 123 1 % {
331 \y 125 2 % }
332 \y 35 6 % #
333 \y 64 11 % @ (letter)
334 \y 39 12 % '
335 \y 40 12 % (
336 \y 41 12 % )
337 \y 42 12 % *
338 \y 45 12 % -
339 \y 46 12 % .
340 \y 47 12 % /
341 \y 91 12 % [
342 \y 93 12 % ]
343 \y 94 7 % ^
344 \y 96 12 % `
345 \y 126 13 % ~
346 \toks0\expandafter{\the\toks0 \relax\noexpand\endinput}%
347 \edef\y#1{\noexpand\expandafter\endgroup%
348 \noexpand\ifx#1\relax \edef#1{\the\toks0}\x\relax%
349 \noexpand\else \noexpand\expandafter\noexpand\endinput%
350 \noexpand\fi}%
351 \expandafter\y\csname luacode@sty@endinput\endcsname%
352 % \end{macrocode}
353 %
354 % Package declaration.
355 %
356 % \begin{macrocode}
357 \ProvidesPackage{luacode}[2012/01/23 v1.2a lua-in-tex helpers (mpg)]
358 % \end{macrocode}
359 %
360 % Make sure Lua\TeX\ is used.
361 %
362 % \begin{macrocode}
363 \RequirePackage{ifluatex}
364 \ifluatex\else
365 \PackageError{luacode}{LuaTeX is required for this package. Aborting.}{%
366 This package can only be used with the LuaTeX engine\MessageBreak
367 (command `lualatex'). Package loading has been stopped\MessageBreak
368 to prevent additional errors.}
369 \expandafter\luacode@sty@endinput
370 \fi
371 % \end{macrocode}
372 %
373 % Use \pk{luatexbase} for catcode tables.
374 %
375 % \begin{macrocode}
376 \RequirePackage{luatexbase}
377 % \end{macrocode}
378 %
379 % \subsection{Internal code}
380 %
381 % Produce Lua code printing debug info for the given argument.
382 %
383 % \begin{macrocode}
384 \newcommand \luacode@printdbg [1] {%
385 texio.write_nl('log',
386 '-- BEGIN luacode debug (on input line \the\inputlineno)')
387 texio.write_nl('log', "\luatexluaescapestring{#1}")
388 texio.write_nl('log',
389 '-- END luacode debug (on input line \the\inputlineno)')
390 }
391 % \end{macrocode}
392 %
393 % Execute a piece of Lua code, possibly printing debug info.
394 % |maybe@printdbg| will be either |printdbg| or |gobble|, see user macros.
395 %
396 % \begin{macrocode}
397 \newcommand \luacode@dbg@exec [1] {%
398 \directlua {
399 \luacode@maybe@printdbg{#1}
400 #1
401 }%
402 }
403 % \end{macrocode}
404 % Execute a piece of code, with shortcuts for double-backslash, percent and
405 % tilde, and trying to preserve newlines. This internal macro is long so
406 % that we can use in the environment, while the corresponding user command
407 % will be short. Make sure |~| is active.
408 %
409 % \begin{macrocode}
410 \begingroup \catcode`\~\active \expandafter\endgroup
411 \@firstofone{%
412 \newcommand \luacode@execute [1] {%
413 \begingroup
414 \escapechar92
415 \newlinechar10
416 \edef\\{\string\\}%
417 \edef~{\string~}%
418 \let\%=\luacode@percentchar
419 \let\#=\luacode@sharpchar
420 \expandafter\expandafter\expandafter\endgroup
421 \luacode@dbg@exec{#1}}
422 }
423 % \end{macrocode}
424 %
425 % Catcode 12 percent and sharp characters for use in the previous command.
426 %
427 % \begin{macrocode}
428 \begingroup \escapechar\m@ne \edef\aux{\endgroup
429 \unexpanded{\newcommand\luacode@percentchar}{\string\%}%
430 \unexpanded{\newcommand\luacode@sharpchar }{\string\#}%
431 }\aux
432 % \end{macrocode}
433 %
434 % Generic code for environments; the argument is the name of a catcode
435 % table. We're normally inside a group, but let's open a new one in case
436 % we're called directly rather that using |\begin|. Define the end marker
437 % to be |\end{<envname>}| with current catcodes.
438 %
439 % \begin{macrocode}
440 \newcommand*\luacode@begin [1] {%
441 \begingroup
442 \escapechar92
443 \luatexcatcodetable#1\relax
444 \edef\luacode@endmark{\string\end{\@currenvir}}%
445 \expandafter\def \expandafter\luacode@endmark \expandafter{%
446 \luatexscantextokens \expandafter{\luacode@endmark}}%
447 \luacode@grab@body}
448 % \end{macrocode}
449 %
450 % We'll define the body grabber in a moment, but let's see how the
451 % environment ends now.
452 %
453 % \begin{macrocode}
454 \newcommand\luacode@end{%
455 \edef\luacode@next{%
456 \noexpand\luacode@execute{\the\luacode@lines}%
457 \noexpand\end{\@currenvir}}%
458 \expandafter\endgroup
459 \luacode@next}
460 % \end{macrocode}
461 %
462 % It is not possible to grab the body using a macro with delimited
463 % argument, since the end marker may contains open-group characters,
464 % depending on the current catcode regime. So we collect it linewise and
465 % check each line against the end marker.
466 %
467 % Storage for lines.
468 %
469 % \begin{macrocode}
470 \newtoks\luacode@lines
471 \newcommand*\luacode@addline [1] {%
472 \luacode@lines\expandafter{\the\luacode@lines#1^^J}}
473 % \end{macrocode}
474 %
475 % Loop initialisation. Set endlinechar explicitely so that we can use it
476 % as a delimiter (and later when writing the code to Lua). Eat up the first
477 % token which is supposed to be a (catcode 12) |\endlinechar| character
478 % token.
479 %
480 % \begin{macrocode}
481 \newcommand \luacode@grab@body [1] {%
482 \luacode@lines{}%
483 \endlinechar10
484 \luacode@grab@lines}
485 % \end{macrocode}
486 %
487 % The actual line-grabbing loop.
488 %
489 % \begin{macrocode}
490 \long\def\luacode@grab@lines#1^^J{%
491 \def\luacode@curr{#1}%
492 \luacode@strip@spaces
493 \ifx\luacode@curr\luacode@endmark
494 \expandafter\luacode@end
495 \else
496 \expandafter\luacode@addline\expandafter{\luacode@curr}%
497 \expandafter\luacode@grab@lines
498 \fi}
499 % \end{macrocode}
500 %
501 % Strip catcode 12 spaces from the beginning of the token list inside
502 % |\luacode@curr|. First we need catcode 12 space, then we procede in the
503 % usual way.
504 %
505 % \begin{macrocode}
506 \begingroup\catcode32 12 \expandafter\endgroup
507 \@firstofone{\newcommand\luacode@spaceother{ }}
508 \newcommand \luacode@strip@spaces {%
509 \expandafter\luacode@strip@sp@peek\luacode@curr\@nil}
510 \newcommand \luacode@strip@sp@peek {%
511 \futurelet\@let@token\luacode@strip@sp@look}
512 \newcommand \luacode@strip@sp@look {%
513 \expandafter\ifx\luacode@spaceother\@let@token
514 \expandafter\@firstoftwo
515 \else
516 \expandafter\@secondoftwo
517 \fi{%
518 \afterassignment\luacode@strip@sp@peek
519 \let\@let@token=
520 }{%
521 \luacode@strip@sp@def
522 }}
523 \@ifdefinable \luacode@strip@sp@def \relax
524 \def \luacode@strip@sp@def #1\@nil{%
525 \def\luacode@curr{#1}}
526 % \end{macrocode}
527 %
528 % Finally, we need a custom catcode table for the default environment:
529 % everything other, except backslash, braces and letters which retain their
530 % natural catcodes.
531 %
532 % Be carefull about the name of the macro for setting catcode ranges which
533 % is currently changing in luatexbase. The group here doesn't matter since
534 % catcode table settings are always global.
535 %
536 % \begin{macrocode}
537 \newluatexcatcodetable \luacode@table@soft
538 \begingroup
539 \ifdefined\SetCatcodeRange \else
540 \let\SetCatcodeRange\setcatcoderange
541 \fi
542 \setluatexcatcodetable \luacode@table@soft {%
543 \luatexcatcodetable\CatcodeTableOther
544 \catcode 92 0
545 \catcode 123 1
546 \catcode 125 2
547 \SetCatcodeRange {65}{90} {11}
548 \SetCatcodeRange {97}{122}{11}
549 }
550 \endgroup
551 % \end{macrocode}
552 %
553 % \subsection{Public macros and environments}
554 %
555 % Debugging.
556 %
557 % \begin{macrocode}
558 \newcommand \LuaCodeDebugOn {\let \luacode@maybe@printdbg \luacode@printdbg}
559 \newcommand \LuaCodeDebugOff {\let \luacode@maybe@printdbg \@gobble}
560 \LuaCodeDebugOff
561 % \end{macrocode}
562 %
563 % The |\luadirect| and |\luaexec| macros.
564 %
565 % \begin{macrocode}
566 \@ifdefinable\luadirect {\let\luadirect\luacode@dbg@exec}
567 \newcommand*\luaexec [1] {\luacode@execute{#1}}
568 % \end{macrocode}
569 %
570 % Environments using different catcode tables.
571 %
572 % \begin{macrocode}
573 \newenvironment {luacode} {\luacode@begin\luacode@table@soft} {}
574 \newenvironment {luacode*} {\luacode@begin\CatcodeTableOther} {}
575 \newcommand \luacodestar {\@nameuse{luacode*}}
576 \def \endluacodestar {\@nameuse{endluacode*}}
577 % \end{macrocode}
578 %
579 % Helper macros
580 %
581 % \begin{macrocode}
582 \newcommand \luastring [1] {"\luatexluaescapestring{#1}"}
583 \newcommand \luastringO [1] {\luastring{\unexpanded\expandafter{#1}}}
584 \newcommand \luastringN [1] {\luastring{\unexpanded{#1}}}
585 % \end{macrocode}
586 %
587 % We're already done!
588 %
589 % \begin{macrocode}
590 \luacode@sty@endinput
591 %</texpackage>
592 % \end{macrocode}
593 %
594 % \section{Test file} \label{test}
595 %
596 % TODO: this test files requires manual checking that the output (pdf and
597 % log file) is correct; this should be fixed.
598 %
599 % \begin{macrocode}
600 %<*testlatex>
601 \documentclass{minimal}
602 \usepackage{luacode}
603 \begin{document}
604
605 \newcommand\foo{3}
606
607 \(
608 \luadirect{
609 texio.write_nl("Special chars: _ ^ & $ { } working.\string\n"
610 .. "Backslashes need a bit of care.\string\n"
611 .. "Sharps and tildes too: # doubled, but \string# and \string~")
612 % a tex comment: no easy way to get a %
613 tex.sprint("\string\\pi \string\\neq", tostring(math.pi))
614 % we can use TeX macros
615 tex.sprint("-", math.sqrt(\foo))
616 }
617 \)
618
619
620 \(
621 \luaexec{
622 texio.write_nl("Special chars: _ ^ & $ { } ~ working.\string\n"
623 .. "Backslashes still need a bit of care.\string\n"
624 .. "Single sharps are easier now: \#")
625 % a tex comment: we also get a % below
626 tex.sprint("\\pi \\neq ", tostring(math.pi):gsub('\%.', '+'))
627 % we can use TeX macros
628 tex.sprint("-", math.sqrt(\foo))
629 }
630 \)
631
632 \[
633 \begin{luacode}
634 texio.write_nl("Special chars: _ ^ & $ { } ~ # % working.\string\n"
635 .. "Only backslashes still need a bit of care.\string\n")
636 -- a lua comment: we could use \% below, too
637 tex.sprint("\\pi \\neq ", tostring(math.pi):gsub('%.', '+'))
638 -- we can use TeX macros
639 tex.sprint("-", math.sqrt(\foo))
640 \end{luacode}
641 \]
642
643 \[
644 \begin{luacode*}
645 texio.write_nl("Special chars: _ ^ & $ { } ~ # % \\ working.\n")
646 -- a lua comment: the backlash is doubled as in normal Lua code
647 tex.sprint("\\pi \\neq ", tostring(math.pi):gsub('%.', '+'))
648 -- no way to use a TeX variable here
649 \end{luacode*}
650 \]
651
652 \newenvironment{mathluacode} { \[ \luacode }{ \endluacode \] }
653 \newenvironment{mathluacode*}{ \[ \luacodestar }{ \endluacodestar \] }
654
655 \begin{mathluacode}
656 local foo = "A full line.\string\n"
657 tex.sprint("\\pi \\neq ", tostring(math.pi):gsub('%.', '+'))
658 -- a lua comment: we could have used \% above, too
659 tex.sprint("-", math.sqrt(\foo))
660 \end{mathluacode}
661
662 \begin{mathluacode*}
663 local foo_bar = "A full line.\n"
664 tex.sprint("\\pi \\neq ", tostring(math.pi):gsub('%.', '+'))
665 -- a lua comment: no way to use a TeX variable here
666 \end{mathluacode*}
667
668 \begin{luacode*}
669 function myfunc(str)
670 assert(type(str) == 'string')
671 tex.sprint(-2, str)
672 end
673 \end{luacode*}
674
675 \newcommand*\mymac [1] {\texttt{\luadirect{myfunc(\luastring{#1})}}\par}
676 \mymac{abc}
677 \mymac{123}
678 \mymac{a"b\string\nc'd}
679
680 \def\mac{\onelevel}
681 \def\onelevel{fully expanded}
682 string : \texttt{\luadirect{myfunc(\luastring \mac)}}\par
683 stringO: \texttt{\luadirect{myfunc(\luastringO\mac)}}\par
684 stringN: \texttt{\luadirect{myfunc(\luastringN\mac)}}\par
685
686 \LuaCodeDebugOn
687 \luadirect {local foo = 'bar' .. \luastring{a"b'c}}
688 \luaexec {local foo = 'bar\%\#' .. \luastring{a"b'c}}
689 \begin{luacode}
690 local foo = 'bar'
691 local baz = 12 % 2
692 assert(\luastring\mac == 'fully expanded')
693 -- assert(\luastringO\mac == '\\onelevel')
694 -- assert(\luastringN\mac == '\\mac')
695 \end{luacode}
696
697 \LuaCodeDebugOff
698 \luadirect{local rem = 'dbg should be disabled here'}
699
700 % \end{macrocode}
701 %
702 % Now track spurious spaces. This is the only part that is automatically
703 % checked, using grep in the Makefile.
704 %
705 % \begin{macrocode}
706 \tracingcommands1
707 \luadirect{local foo}%
708 \luaexec{local foo}%
709 \begin{luacode}
710 local foo
711 \end{luacode}
712 \begin{luacode*}
713 local foo
714 \end{luacode*}
715 \tracingcommands0
716
717 \end{document}
718 %</testlatex>
719 % \end{macrocode}
720 %
721 %
722 % \Finale
723 \endinput