数组和管道的问题

数组和管道的问题

我尝试用软件包创建自己的环境array并使用>{...}。当描述符以竖线“|”开头时,我遇到了问题:! Package array Error: >{..} at wrong position: token ignored.。我尝试使用\expandafter\@ifnextchar,但徒劳无功。

\documentclass{article}
\usepackage{array}

\makeatletter
\newcount\c@untligne
\def\numligne{\ifnum\c@untligne<\@ne\relax%
              \else
                    \the\c@untligne%
              \fi\global\advance\c@untligne\@ne}
\makeatother

\newenvironment{mytabular}[1]{\begin{tabular}{>{\numligne}#1}}{\end{tabular}}

\begin{document}
\begin{mytabular}{c|c} % here when |c|c|
\hline
    row & column\\
    & ligne \\ 
    & ligne\\ 
\hline 
\end{mytabular}
\end{document}

答案1

您想要检查一个主要论点|

\documentclass{article}
\usepackage{array}

\newcounter{rowcount}
\newcommand{\numberrow}{%
  \ifnum\value{rowcount}>0
    \therowcount
  \fi
  \stepcounter{rowcount}%
}

\makeatletter
\newenvironment{mytabular}[1]
  {\mytabular@look#1\@nil}
  {\end{tabular}\setcounter{rowcount}{0}}
\newcommand{\mytabular@look}{%
  \@ifnextchar|{\mytabular@look@i}{\mytabular@look@ii}%
}
\def\mytabular@look@i#1#2\@nil{%
  \begin{tabular}{|>{\numberrow}#2}%
}
\def\mytabular@look@ii#1\@nil{%
  \begin{tabular}{>{\numberrow}#1}%
}
\makeatother

\begin{document}
\begin{mytabular}{cc}
\hline
    row & column\\
    & ligne \\ 
    & ligne\\ 
\hline 
\end{mytabular}

\bigskip

\begin{mytabular}{|c|c|}
\hline
    row & column\\
    & ligne \\ 
    & ligne\\ 
\hline 
\end{mytabular}
\end{document}

在此处输入图片描述

相关内容