# How to give a header to the blank column?

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}


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?

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 &.

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

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
}

{%
\myStrCMP{#1}{c}{\hfill#2\hfill}
{%
\myStrCMP{#1}{r}{\hfill#2}{#2\hfill}%
}
}
{%
\@ifnextchar[
}
{%
\myStrCMP{#1}{r}
{%
\myStrCMP{#1}{c}
{%
{%
\parbox
{\dimexpr#2-2\tabcolsep\relax}{\centering#3}%
}%
}
{%
\myStrCMP{#1}{j}
{%
}
{%
{\parbox{\dimexpr#2-2\tabcolsep\relax}{\raggedright#3}}%
}
}%
}%
}
{%
\rlap{\hskip2\tabcolsep#1}%
}
\makeatother

\begin{document}
\begin{tabular}[]{|l|@{\hspace{5em}}|}
\hline
\hline
\hline
B\\
\hline
\end{tabular}

\begin{tabular}[]{|l|@{\hspace{5em}}|}
\hline
\hline
\hline
B\\
\hline
\end{tabular}

\begin{tabular}[]{|l|@{\hspace{5em}}|}
\hline
\hline
\hline
B\\
\hline
\end{tabular}

\begin{tabular}[]{|l|@{\hspace{5em}}|}
\hline
\hline
\hline
B\\
\hline
\end{tabular}
\end{document}


Result:

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}

• see this pasteboard.co/HbDrymm.png Mar 13 '18 at 1:14
• Or use no vertical line and put header for each column. Mar 13 '18 at 1:21