3

I'm working on a new environment based on tabularx and using some luatex functions. Actually, I have the following code.

\documentclass[10pt,oneside]{article}
\usepackage{luatextra}
\usepackage{tabularx}

\begin{luacode*}
  function fline(value)
    n = value
    tex.print([[\multicolumn{1}{c}{} & \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} \\\cline{2-3}]])
  end
  function nline(input)
    n = n+1
    tex.print(tostring(n),[[ & ]],input,[[ \\\cline{2-3}]])
  end
\end{luacode*}

\newenvironment{calc}[3][A1]{%
\newcolumntype{x}{>{\centering\arraybackslash}X}%
\newcommand{\fline}{\luaexec{fline(0)}}
\newcommand{\nline}[1]{\luaexec{nline(\luastring{##1})}}
\tabularx{#2}{c#3}\fline}{%
\endtabularx}

\begin{document}

\begin{calc}{5cm}{|x|x|}
\nline{a & b}
\nline{c & d}
\end{calc}

The problem is that the \multicolumn{1}{c}{} in fline function generates a Misplaced \omit error. I don't really understand why and what kind of code should be working. Thanks for any help about this.

1
  • @DavidCarlisle : the [[ characters are lua string delimiters so normally tex doesn't see them.
    – MB4E
    May 5, 2016 at 10:00

2 Answers 2

3

\directlua is expandable but \luaexec is defined via

> \luacode@execute=\long macro:
#1->\begingroup \escapechar 92 \newlinechar 10 \edef \\{\string \\}\edef ~{\str
ing ~}\let \%=\luacode@percentchar \let \#=\luacode@sharpchar \expandafter \exp
andafter \expandafter \endgroup \luacode@dbg@exec {#1}.

so inserts many non expandable tokens before the \multicolumn

It is usually easier to use the primitive directly:

\documentclass[10pt,oneside]{article}
%%%\usepackage{luatextra}
\usepackage{tabularx}

\directlua{\unexpanded{%
  function fline(value)
    n = value
    tex.print([[\multicolumn{1}{c}{} & \multicolumn{1}{c}{1} & \multicolumn{1}{c}{2} \\\cline{2-3}]])
  end
  function nline(input)
    n = n+1
    tex.print(tostring(n),[[ & ]],input,[[ \\\cline{2-3}]])
  end
}}

\newenvironment{calc}[3][A1]{%
\newcolumntype{x}{>{\centering\arraybackslash}X}%
\newcommand{\fline}{\directlua{fline(0)}}
\newcommand{\nline}[1]{\directlua{nline("\luaescapestring{#1}")}}
\tabularx{#2}{c#3}\fline}{%
\endtabularx}

\begin{document}


\begin{calc}{5cm}{|x|x|}
\nline{a & b}
\nline{c & d}
\end{calc}

\end{document}
0
0

Here's a solution that (a) uses \directlua instead of \luaexec and (b) doesn't use [[ and ]] to delimit Lua strings.

\documentclass{article}
\usepackage{luacode}
\usepackage{tabularx}

\begin{luacode*}
  function fline(value)
    n = tonumber(value)
    tex.print("\\multicolumn{1}{c}{} & \\multicolumn{1}{c}{1} & \\multicolumn{1}{c}{2} \\\\\\cline{2-3}")
  end
  function nline(input)
    n = n+1
    tex.print(tostring(n),"&",input,"\\\\\\cline{2-3}")
  end
\end{luacode*}

\newcolumntype{x}{>{\centering\arraybackslash}X}%
\newcommand{\fline}{\directlua{fline(0)}}
\newcommand{\nline}[1]{\directlua{nline(\luastring{#1})}}
\newenvironment{calc}[3][A1]{%
   \tabularx{#2}{c#3}
   \fline}{%
   \endtabularx}

\begin{document}

\begin{calc}{5cm}{|x|x|}
  \nline{a & b}
  \nline{c & d}
\end{calc}

\end{document}
2
  • Thanks. Why do not use brackets delimiters ?
    – MB4E
    May 6, 2016 at 7:51
  • @MB78 - I find it easier to read the Lua code without the double brackets. Clearly, this is mostly a matter of personal preference.
    – Mico
    May 6, 2016 at 7:56

You must log in to answer this question.

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