2

From this site I found a table code where a column is blank. And there he didn't use & for each rows, just put a \hspace to the header.

\documentclass{article}

\begin{document}

\begin{tabular}{|l|@{\hspace{1em}}|}
  \hline
  A \\
  \hline
  B \\
  \hline
\end{tabular}

\begin{tabular}{|l|@{\hspace{2em}}|}
  \hline
  A \\
  \hline
  B \\
  \hline
\end{tabular}

\end{document}  

enter image description here

Source: How to make enough space in empty column?

My question is I want to make this type of table(no necessary the vertical line) where some columns are blanked, but have the header. And I don't want to put & for each rows. I want to do something like \begin{tabular}{|l|@\header{Quantity{\hspace{1em}}}|}
I need this for the question How to make good market/shopping/grocery list in LaTeX?

3

If it's the last column that doesn't have any content, and if vertical rules are not required, you can just do this:

\documentclass{article}
\begin{document}

\begin{tabular}{ll}
Name & Quantity \\
A \\
B \\
C
\end{tabular}

\end{document}

You can end a table row with \\ before the last column, it's not required to add all the &.

output of code

1
  • I tried something similar of this, but error was occurred for something.
    – alhelal
    Mar 13 '18 at 13:05
2

A hacky way of doing it, but the following should work in most cases. \myheader takes:

  • one optional argument specifying the alignment where r right-aligns, c centres, and everything else left-aligns.
  • After that optional argument follows a mandatory argument containing the contents of the header of the first column.
  • After that there is an optional argument that needs to be entered as [<alignment>,<width>], where <alignment> again is r for right-alignment, c for centred or j for justified and everything else is left-aligned with fixed <width>. If the optional argument is not given, the content is left aligned, but without fixed width.
  • And finally the contents of the second column header

Code:

\documentclass[]{article}

\makeatletter
\newcommand\myStrCMP[2]
  {%
    \bgroup
      \def\tmpa{#1}%
      \def\tmpb{#2}%
      \ifx\tmpa\tmpb
        \egroup
        \expandafter\@firstoftwo
      \else
        \egroup
        \expandafter\@secondoftwo
      \fi
  }

\newcommand\myheader[2][l]
  {%
    \myStrCMP{#1}{c}{\hfill#2\hfill}
      {%
        \myStrCMP{#1}{r}{\hfill#2}{#2\hfill}%
      }
    \myheader@i
  }
\def\myheader@i%
  {%
    \@ifnextchar[
      {\myheader@ii}
      {\myheader@iii}%
  }
\long\def\myheader@ii[#1,#2]#3%
  {%
    \myStrCMP{#1}{r}
      {\myheader@iii{\parbox{\dimexpr#2-2\tabcolsep\relax}{\raggedleft#3}}}
      {%
        \myStrCMP{#1}{c}
          {%
            \myheader@iii
              {%
                \parbox
                  {\dimexpr#2-2\tabcolsep\relax}{\centering#3}%
              }%
          }
          {%
            \myStrCMP{#1}{j}
              {%
                \myheader@iii{\parbox{\dimexpr#2-2\tabcolsep\relax}{#3}}%
              }
              {%
                \myheader@iii
                  {\parbox{\dimexpr#2-2\tabcolsep\relax}{\raggedright#3}}%
              }
          }%
      }%
  }
\newcommand\myheader@iii[1]
  {%
    \rlap{\hskip2\tabcolsep#1}%
  }
\makeatother

\begin{document}
\begin{tabular}[]{|l|@{\hspace{5em}}|}
  \hline
  \myheader{1col}{2col}\\
  \hline
  Abc is longer than header\\
  \hline
  B\\
  \hline
\end{tabular}

\begin{tabular}[]{|l|@{\hspace{5em}}|}
  \hline
  \myheader[c]{1col}[c,5em]{2col}\\
  \hline
  Abc is longer than header\\
  \hline
  B\\
  \hline
\end{tabular}

\begin{tabular}[]{|l|@{\hspace{5em}}|}
  \hline
  \myheader[r]{1col}[r,5em]{2col}\\
  \hline
  Abc is longer than header\\
  \hline
  B\\
  \hline
\end{tabular}

\begin{tabular}[]{|l|@{\hspace{5em}}|}
  \hline
  \myheader[l]{1col}[l,5em]{2col with long header}\\
  \hline
  Abc is longer than header\\
  \hline
  B\\
  \hline
\end{tabular}
\end{document}

Result:

enter image description here

1

You can use \multicolumn.

\documentclass{article}

\begin{document}

\begin{tabular}{|l|@{\hspace{6em}}|}
  \hline
  \multicolumn{1}{|r|}{Quantity}\\
  \hline
  A \\
  \hline
  B \\
  \hline
\end{tabular}

\end{document}
2

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.