10 定义一个命令,接受参数为数组,命令会读取数组中的列数并把列数传递给\@columnnum,如何使用LaTeX3读取?

发布于 2024-09-16 16:08:01
  • 如题,定义的命令\foo会吃掉一个二维数组参数,让listofitems包读取数组,并使用该包的功能输出每一行的元素个数(列数),经过比较后,将数组中最大列数传递给\@columnnum.
  • 已知\listlen\@foo[1]\listlen\@foo[1]会分别获取数组中第一行和第二行的元素个数(列数).
  • 首先使用\ifnum比较大小这种foolish的方法获取其中的最大值并传递给\@columnnuma,在\begin{document}后使用\@columnnuma成功输出该数组的最大列数.
  • 但是如果用expl3\int_max:nn{express1}{express2}进行比较并把输出结果传递给\@columnnumb后,正文中\@columnnumb则无法正常输出列数最大值. 报错为`Missing $ inserted.

<inserted text>`(我ExplSyntaxOn也没落下啊😫)

The following is MWE:

\documentclass{minimal}

\usepackage{listofitems}
\setsepchar{;/,}
\ignoreemptyitems

\makeatletter
\NewDocumentCommand{\foo}{m}{
    \readlist*\@foo{#1}
    \ifnum \listlen\@foo[1]<\listlen\@foo[2]
        \gdef\@columnnuma{\listlen\@foo[2]}
    \else
        \gdef\@columnnuma{\listlen\@foo[1]}
    \fi
    \gdef\@columnnumb{
        \ExplSyntaxOn
        \int_max:nn{\listlen\@foo[1]}{\listlen\@foo[2]}
        \ExplSyntaxOff    
    }
}

\begin{document}

\foo{1,2,3,4,5;6,7,8,9}

There are \@columnnuma{} columns in the group.

% There are \@columnnumb{} columns in the group.

\ExplSyntaxOn
There~are~\int_max:nn{\listlen\@foo[1]}{\listlen\@foo[2]} ~columns~in~the~group.
\ExplSyntaxOff
\makeatother

\end{document}

查看更多

关注者
1
被浏览
175
1 个回答
Skyrmion
Skyrmion 3天前
Looking forward to my Ph.D. offer...

感谢群友🐟老师 ExplSyntax包定义外面即可

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览