Site icon texblog

Removing/hiding a column in a LaTeX table

It’s easy to add, remove, or shift rows. Just copy-and-paste or delete the corresponding line in the table. Removing columms is a different story. You might be able to come up with some sophisticated regular expression, which removes the right value in every row. Otherwise, the only way is to use another program or to do it manually. Also, what if you later decide to include the column?

Here is a neat way to hide table columns. This trick requires minimal changes to the table and can be undone easily.

Define a new column type that swallows its content.

The array package implements a macro to define your own column type. This is useful when complicated column styles are reused multiple times. Here, we will use it to hide the column content.

The following code defines a new column type H that packs its content into a box of zero size. To remove the intercolumn space after the column we use: @{}.

%In the preamble
\usepackage{array}
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}

Now we can hide/unhide columns by using the newly defined column type H.

\begin{tabular}{lcrH}
...
\end{tabular}

 
An example

Assume the following table that lists cancer incidences for men, women, and both sexes.

 
Using the trick above, it becomes really easy to hide the six columns in the middle and only show the numbers for both sexes.

The example also illustrates that this works with \multicolumn. All I did is to set the column type to H.

 
Complete code for the example

\documentclass[11pt]{article}
\usepackage{multirow}
\usepackage{array}
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}
\pagestyle{empty}
\begin{document}
\begin{table}[ht]
\caption{All cancers: estimated incidence, mortality, and prevalence worldwide in 2012.}
\begin{center}
\begin{tabular}{l*{6}{H}*{3}{r}}
\hline
\multirow{2}{*}{Estimated numbers (thousands)}&\multicolumn{3}{H}{Men}&\multicolumn{3}{H}{Women}&\multicolumn{3}{c}{Both sexes}\\\cline{2-10}
&Cases&Deaths&5-year prev.&Cases&Deaths&5-year prev.&Cases&Deaths&5-year prev.\\
\hline
World&7427&4653&15362&6663&3548&17182&14090&8201&32545\\
More developed regions&3244&1591&8616&2832&1287&8297&6076&2878&16913\\
Less developed regions&4184&3062&6747&3831&2261&8885&8014&5323&15632\\
WHO Africa region&265&205&468&381&250&895&645&456&1363\\
WHO Americas region&1454&677&3843&1429&618&4115&2882&1295&7958\\
WHO East Mediterranean region&263&191&461&293&176&733&555&367&1194\\
WHO Europe region&1987&1080&4857&1750&852&4933&3737&1932&9790\\
WHO South-East Asia region&816&616&1237&908&555&2041&1724&1171&3278\\
WHO Western Pacific region&2642&1882&4493&1902&1096&4464&4543&2978&8956\\
IARC membership&3706&1900&9259&3354&1570&9425&7060&3470&18684\\
United States of America&825&324&2402&779&293&2373&1604&617&4775\\
China&1823&1429&2496&1243&776&2549&3065&2206&5045\\
India&477&357&665&537&326&1126&1015&683&1790\\
European Union&1446&715&3759&1211&560&3487&2657&1276&7246\\
\hline
\end{tabular}
\label{tab:globocan}
\end{center}
\end{table}%
\end{document}

 
I first came across this trick here.

Data for the example is available online and was modified from Globocan.

Exit mobile version