2

I try to make my own environment with the package array and use >{...}. When the descriptor begins with a pipe "|", I have a problem : ! Package array Error: >{..} at wrong position: token ignored.. I try to use \expandafter or \@ifnextchar, in vain.

\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}
4
  • Welcome to TeX.SX! >{} does nothing at all, except producing an error when #1 starts with |. – egreg Aug 15 '17 at 16:24
  • @egreg, I know, I have a macro in >{} but not necessary for MWE, I want to be able du use | in first place in argument of mytable – nikopol Aug 15 '17 at 16:37
  • @nikopol you might add the command to your MWE (or at least say what you want to do)! This way someone might think of a more robust way of doing that! – Skillmon Aug 15 '17 at 17:30
  • @Skillmon, I edit my question – nikopol Aug 15 '17 at 17:43
4

You want to examine the argument for a leading |:

\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}

enter image description here

3
  • That's what I want to do, without success. Thx – nikopol Aug 15 '17 at 18:20
  • Is there a way to do the same with \expandafter before >{}? – nikopol Aug 17 '17 at 10:17
  • @nikopol No: with array no expansion is performed on the argument to tabular. – egreg Aug 17 '17 at 10:17

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.