I would like to know if it is possible to let LaTeX Table intepret && as &0&. So the blank is not the literal NA or any corresponding here. Code



\begin{tabular}{| l | l | l |}
AHB        && \\ \hline


I change the table row to

AHB       1 && 1 \\ \hline

I would like it be interpreted

AHB       1 &0& 1 \\ \hline

You can collect the cell contents and test whether it is blank:

enter image description here


\newcommand{\emptytozero}[1]{% https://tex.stackexchange.com/q/53068/5764


\begin{tabular}{ c | >{\collectcell\emptytozero}c<{\endcollectcell} }
   &   \\
   & 1 \\
 1 &   \\
 1 & 1


If multiple columns are to be examined, one can define a new column type (collcell loads array.) \newcolumntype:

\newcolumntype{C}{ >{\collectcell\emptytozero}c<{\endcollectcell} }
|improve this answer|||||
  • Can you please say if this method can also be used a shorthand form like \begin{tabular}{| p | C |} where C is about your \newcolumntype? – Léo Léopold Hertz 준영 Nov 18 '16 at 11:18
  • 2
    @Masi: Yes it can. That's what I suggest at the end of my answer. – Werner Nov 18 '16 at 16:05

Here's a very simple version that fills in blank tabular cells with a zero. The check is performed by a new column format z which should be used for all columns that have to be filled with zeros if necessary.


\def\zeroIfEmpty#1\endzero{\setbox0=\hbox{#1}\ifdim\wd0=0pt 0\else#1\fi}

\begin{tabular}{| z | z | z | l }
   &   &   &\\
   &   & 1 &\\
   & 1 &   &\\
 1 &   &   &\\
 1 & 1 & 1 &\\

enter image description here

Note that this is a very simple check which is likely to break in several more complicated situations, e.g., it does break if used in the last column of a tabular (which can be fixed by an extra empty column as shown in the example). So it's probably better to not use it at all. ;-)

|improve this answer|||||
  • How does your approach differ from Werner's one? Your newcolumntype sentence is little longer here. – Léo Léopold Hertz 준영 Nov 18 '16 at 10:05
  • 1
    @Masi My version simply wraps the zero-or-not-zero check around the standard column format. This has the downside that I can't check for an empty cell directly because the standard formats silently add some commands to cancel extra space at the beginning and end of each cell (note the use of a temporary \hbox to circumvent the effects of these commands). OTOH, Werner's version uses the collcell package that seems to take more care to remove these extra commands. And it also works when used in the last column of a tabular, which makes his solution the better one, in my eyes. – siracusa Nov 18 '16 at 10:24

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.