4

I need to create a command that goes \MyCommand{x,y} or \MyCommand{x,y,z} (both ways should be possible), where x, y, and z are three numbers. It should give the output \begin{matrix}x\\y\end{matrix} and \begin{matrix}x\\y\\z\end{matrix}, respectively.

And the command should have just one argument in a comma separated form as represented.

I'm not very familiar with using Lua code in LaTeX. But I tried the following any way.

\newcommand*{\MyCommand}[1]{%
\directlua {
  str={#1}
    if str[3]==nil then
       tex.sprint([[\begin{matrix}]] .. str[1] .. [[\\]] .. str[2] .. [[\\]] .. [[\end{matrix}]])
    else
       tex.sprint([[\begin{matrix}]] .. str[1] .. [[\\]] .. str[2] .. [[\\]] .. str[3] .. [[\end{matrix}]])
    end
}
}%

Please let me know where I am mistaken, or how I can develop this command.

I am using MikTeX distribution and LuaLaTeX output mode.

Here's a MWE

\documentclass{article}
\usepackage{amsmath}
\newcommand*{\MyCommand}[1]{%
\directlua {
  str={#1}
    if str[3]==nil then
       tex.sprint([[\begin{matrix}]] .. str[1] .. [[\\]] .. str[2] .. [[\\]] .. [[\end{matrix}]])
    else
       tex.sprint([[\begin{matrix}]] .. str[1] .. [[\\]] .. str[2] .. [[\\]] .. str[3] .. [[\end{matrix}]])
    end
}
}%

\begin{document}

$\MyCommand{x,y}$\\
$\MyCommand{x,y,z}$

\end{document}
  • do you need lua for some reason not shown (you could just as easily do it in tex) but if you just want lua answers that's OK. – David Carlisle Jul 4 '17 at 8:09
  • If there is another way that would preserve the command as it is (\MyCommand{x,y} or \MyCommand{x,y,z}), it would be great. – ένας Jul 4 '17 at 8:12
  • It's just a chance to better understand Lua though .. :) – ένας Jul 4 '17 at 8:13
6

I would just do this in TeX (note you omitted amsmath)

\documentclass{article}
\usepackage{amsmath}
\newcommand*{\MyCommand}[1]{\begin{matrix}\relax\zz#1,\end{matrix}}
\def\zz#1,#2{#1\ifx\end#2\else\expandafter\\\expandafter\zz\fi#2}

\begin{document}


$\MyCommand{x,y}$

$\MyCommand{x,y,z}$

\end{document}

For the Lua version you need to remember that macros expand before being passed to lua so you could put \unexpanded{....} around the full argument but then you also would need the elements of your table to be the lua string 'x' not (as you have it) the undefined lua variable x so you would need an extra layer of string quoting if done that way. Or more simply treat the whole thing as a string and just replace , by \\ using a Lua replace

\documentclass{article}
\usepackage{amsmath}
\newcommand*{\MyCommand}[1]{%
\begin{matrix}%
\directlua{
s=string.gsub('\luaescapestring{#1}',',','\string\\\string\\')
tex.sprint(s)}%
\end{matrix}}

\begin{document}



$\MyCommand{x,y}$

$\MyCommand{x,y,z}$

\end{document}
  • note both these versions allow any number of commas not just 2 or 3. – David Carlisle Jul 4 '17 at 9:11
6

Another simple version (but with expl3). For the image I have replaced matrix with pmatrix. It's extensible too (comma list element count is not specified).

matrix

\documentclass{article}
\usepackage{amsmath}
\usepackage{expl3,xparse}
\ExplSyntaxOn
\NewDocumentCommand { \MyCommand } { m }
    {
        \begin{matrix}
            \seq_set_from_clist:Nn \l_tmpa_seq { #1 }
            \seq_use:Nn \l_tmpa_seq { \\ }
        \end{matrix}
    }
\ExplSyntaxOff

\begin{document}

$\MyCommand{x,y}$

$\MyCommand{x,y,z}$

$\MyCommand{x,y,z,a,b}$

\end{document}
2

The following answer is very similar to David's. The main difference is the use of the luacode package, which provides an environment called luacode. Inside a luacode environment, most of TeX's "special" characters -- #, $, %, ^, &, {, }, ~ -- can be entered directly. (This is particularly useful for the % character, which as its own special meaning in some of Lua's pattern-matching and math-related functions. The only one of TeX's "special" characters that needs, well, special treatment is the backslash character, \: it has to be entered as \\. To enter a double-backslash, one has to type \\\\. Finally, Lua-style line comments are possible inside a luacode environment.

On a separate note: Observe the use of the macro \luastringN, which is also provided by the luacode package, to pass the argument of the LaTeX macro to the Lua function. In my view, \luastringN{#1} is easier to remember (and certainly easier on the eyes...) than '\luaescapestring{\unexpanded{#1}}' is. :-)

enter image description here

% !TeX program = lualatex
\documentclass{article}
\usepackage{luacode} % for 'luacode' env. and '\luastringN' macro

%% Lua-side code
\begin{luacode}
function comma2double_backslash ( s )
   -- replace all commas with double-backslashes
   tex.sprint ((s:gsub ( ',' , '\\\\' )))
end
\end{luacode}
%% TeX-side code
\newcommand*{\MyCommand}[1]{%
   \begin{array}{@{}c@{}}
   \directlua{comma2double_backslash ( \luastringN{#1} )}
   \end{array}%
}

\begin{document}
$\MyCommand{x,y}$

$\MyCommand{x,y,z}$
\end{document}

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.