表格内的 `\readlist{}` 不起作用

表格内的 `\readlist{}` 不起作用

我正在编写一个包,其中包含以下代码作为其一部分。问题在于 longtable 环境中的 \readlist。当我编写代码时,不是作为宏而是在主体中,它可以工作,在这种情况下,我在 longtable 环境之外编写 \readlist。我想在表中使用它,请帮我解决。

这是宏代码,有错误

% !TeX program = xelatex
\documentclass{article}
\usepackage{fontspec}
\usepackage{etextools}% for entry
\usepackage{longtable}
\usepackage{xparse}
\usepackage{xstring}
\usepackage{listofitems}
\setmainfont[Script=Devanagari,Mapping=devanagarinumerals]{Shobhika}

\NewDocumentCommand\separateswarbol{m}{\StrBetween{#1}{(}{)}[\swars] \StrBetween{#1}{[}{]}[\bols]}

\NewDocumentCommand\myformat{m}{&#1}

\NewDocumentCommand\entry{m}{
    \edef\result{\csvloop[\myformat]{#1}}
    \result \\}


\newcounter{colnum}
\newcounter{restart}
\newcounter{ithunsuru}
\NewDocumentCommand\maketabularrow{mO{1}}{%
    \setcounter{colnum}{0}%
    \setcounter{ithunsuru}{#2}%
    \setcounter{restart}{1}
    \makeatletter
    \whileboolexpr
    { test {\ifnumcomp{\value{colnum}}{<}{#1}} }%
    { &\ifnum\value{ithunsuru}<#1 \theithunsuru \stepcounter{ithunsuru}\else \ifnum\value{ithunsuru}=#1 \theithunsuru \stepcounter{ithunsuru} \else  \therestart \stepcounter{restart}\fi \fi \stepcounter{colnum}
    }
}

\newcounter{kitishift}
\newcounter{countar}
\NewDocumentCommand\shiftrow{O{0}}{
    \setcounter{kitishift}{#1}
    \setcounter{countar}{1}

    \whileboolexpr
    { test {\ifnumcomp{\value{countar}}{<}{\value{kitishift}}} }%
    {& \stepcounter{countar}} 
    \setcounter{kitishift}{0}
    \setcounter{countar}{0}
}

\NewDocumentEnvironment{taal}{mO{1}}
{   

    \begin{longtable}{ c@{} *{#1}{c|} } 

        \maketabularrow{#1}[#2]\\

    }{\end{longtable}

}

\NewDocumentCommand\khand{O{0}m}{%
    \separateswarbol{#2}
    \readlist*\bollist{\bols}
    \readlist*\swarlist{\swars}
    \newcounter{swarstringlen}
    \newcounter{bolstringlen}
    \newcounter{spos}
    \newcounter{bpos}
    \StrCount{\swars}{,}[\swarlen]
    \StrCount{\bols}{,}[\bollen]
    \setcounter{swarstringlen}{\swarlen}
    \stepcounter{swarstringlen}
    \stepcounter{swarstringlen}
    \setcounter{bolstringlen}{\bollen}  
    \stepcounter{bolstringlen}
    \stepcounter{bolstringlen}
    \setcounter{spos}{1}
    \setcounter{bpos}{1}

    \shiftrow[#1]
    \whileboolexpr
    {test {\ifnumcomp{\value{spos}}{<}{\value{swarstringlen}}}}%
    {&\swarlist[\thespos]\stepcounter{spos}}\\
    \shiftrow[#1]
    \whileboolexpr
    {test {\ifnumcomp{\value{bpos}}{<}{\value{bolstringlen}}}}%
    {&\bollist[\thebpos]\stepcounter{bpos}} 
}

\begin{document}
    \begin{taal}{16}[9]

        \khand[1]{(प,{धंध},j, नीं)[x,z,c,v] }\\

    \end{taal}
\end{document}

这是体内可以起作用的代码。

% !TeX program = xelatex
\documentclass{article}
\usepackage{fontspec}
\usepackage{etextools}% for entry
\usepackage{longtable}
\usepackage{xparse}
\usepackage{xstring}
\usepackage{listofitems}
\setmainfont[Script=Devanagari,Mapping=devanagarinumerals]{Shobhika}

\NewDocumentCommand\separateswarbol{m}{\StrBetween{#1}{(}{)}[\swars] \StrBetween{#1}{[}{]}[\bols]}

\NewDocumentCommand\myformat{m}{&#1}

\NewDocumentCommand\entry{m}{
    \edef\result{\csvloop[\myformat]{#1}}
    \result \\}


\newcounter{colnum}
\newcounter{restart}
\newcounter{ithunsuru}
\NewDocumentCommand\maketabularrow{mO{1}}{%
    \setcounter{colnum}{0}%
    \setcounter{ithunsuru}{#2}%
    \setcounter{restart}{1}
    \makeatletter
    \whileboolexpr
    { test {\ifnumcomp{\value{colnum}}{<}{#1}} }%
    { &\ifnum\value{ithunsuru}<#1 \theithunsuru \stepcounter{ithunsuru}\else \ifnum\value{ithunsuru}=#1 \theithunsuru \stepcounter{ithunsuru} \else  \therestart \stepcounter{restart}\fi \fi \stepcounter{colnum}
    }
}

\newcounter{kitishift}
\newcounter{countar}
\NewDocumentCommand\shiftrow{O{0}}{
    \setcounter{kitishift}{#1}
    \stepcounter{kitishift}
    \setcounter{countar}{1}

    \whileboolexpr
    { test {\ifnumcomp{\value{countar}}{<}{\value{kitishift}}} }%
    {& \stepcounter{countar}} 
    \setcounter{kitishift}{0}
    \setcounter{countar}{0}
}

\NewDocumentEnvironment{taal}{mO{1}}
{   

    \begin{longtable}{ c@{} *{#1}{c|} } 

        \maketabularrow{#1}[#2]\\

    }{\end{longtable}

}

\begin{document}

\separateswarbol{(प,{धंध},j, नीं)[x,z,c,v]}
\readlist*\bollist{\bols}
\readlist*\swarlist{\swars}

\begin{taal}{16}[9]
\newcounter{swarstringlen}
\newcounter{bolstringlen}
\newcounter{spos}
\newcounter{bpos}
\StrCount{\swars}{,}[\swarlen]
\StrCount{\bols}{,}[\bollen]
\setcounter{swarstringlen}{\swarlen}
\stepcounter{swarstringlen}
\stepcounter{swarstringlen}
\setcounter{bolstringlen}{\bollen}  
\stepcounter{bolstringlen}
\stepcounter{bolstringlen}
\setcounter{spos}{1}
\setcounter{bpos}{1}
\shiftrow[2]
\whileboolexpr
{ test {\ifnumcomp{\value{spos}}{<}{\value{swarstringlen}}} }%
{&\swarlist[\thespos]\stepcounter{spos}} \\
\shiftrow[2]
\whileboolexpr
{ test {\ifnumcomp{\value{bpos}}{<}{\value{bolstringlen}}} }%
{&\bollist[\thebpos]\stepcounter{bpos}} 
\end{taal}
\end{document}

谢谢你!

答案1

这是一个仅使用 的更简单的版本expl3

\documentclass{article}
\usepackage[a4paper,margin=1cm]{geometry}
\usepackage{fontspec}
\usepackage{longtable}
\usepackage{xparse}

\setmainfont{Shobhika}[
  Script=Devanagari,
  Mapping=devanagarinumerals
]

\ExplSyntaxOn
\NewDocumentEnvironment{taal}{ m O{1} }
 {
  \int_set:Nn \l__girish_khand_length_int { #1 }
  \begin{longtable}{ c @{} *{#1}{c|} }
  % make the first row
  \int_step_function:nnN { #2 } { #1 } \__girish_khand_entry:n
  \int_step_function:nnN { 1 } { #2 - 1 } \__girish_khand_entry:n
  \\
 }
 {\end{longtable}}

\NewDocumentCommand{\khand}{ O{0} r() r[] }
 {
  \girish_khand:nnn { #1 } { #2 } { #3 }
 }

\int_new:N \l__girish_khand_length_int
\seq_new:N \l__girish_khand_seq

\cs_new_protected:Nn \girish_khand:nnn
 {
  % make the first row of \khand
  \__girish_khand_row:nn { #1 } { #2 } \\
  % make the first row of \khand
  \__girish_khand_row:nn { #1 } { #3 } \\
 }

\cs_new_protected:Nn \__girish_khand_row:nn
 {
  \seq_set_split:Nnn \l__girish_khand_seq { , } { #2 }
  \prg_replicate:nn { #1 } { \seq_put_left:Nn \l__girish_khand_seq { } }
  \prg_replicate:nn { \l__girish_khand_length_int - #1 - \clist_count:n { #2 } }
                    { \seq_put_right:Nn \l__girish_khand_seq { } }
  \seq_map_function:NN \l__girish_khand_seq \__girish_khand_entry:n
 }

\cs_new:Nn \__girish_khand_entry:n { & #1 }

\ExplSyntaxOff

\begin{document}

\begin{taal}{16}[9]
\khand(प,{धंध},j, नीं)[x,z,c,v]
\khand[1](प,{धंध},j, नीं)[x,z,c,v]
\khand[2](प,{धंध},j, नीं)[x,z,c,v]
\khand[4](प,{धंध},j, नीं)[x,z,c,v]
\end{taal}

\end{document}

注意语法的变化:括号(...,...)[...,...]不是必需的。可以修改为与原文相同。

两个主要参数\khand存储在一个序列中,该序列由左侧和右侧的空条目补充,以等于环境设置的列数taal

使用\seq_map_function:NN可确保结果“一次性”生成,因此在实际“看到”单元格之前,表格单元格不会发生变化。换句话说,当 TeX 位于第一个单元格时,行就会生成。您的问题确实是您在一个单元格中开始循环,但在另一个单元格中结束,这是不可能的。

在此处输入图片描述

相关内容