1

I'm making a table (similar to a calendar) in which I have a date or some short string in the upper right of each table cell, and then have the contents of that cell left aligned.

I am trying to write a little \newcommand to help make this simpler to do.

Here's the long way to do my table, but it comes out correct:

\begin{tabular}{|l|l|l|}
\hline
\multicolumn{1}{|r|}{\textbf{1}} & \multicolumn{1}{r|}{\textbf{2}} & \multicolumn{1}{r|}{\textbf{3}} \\
first thing & second thing & third thing \\
\hline
\multicolumn{1}{|r|}{\textbf{4}} & \multicolumn{1}{r|}{\textbf{New 1}} & \multicolumn{1}{r|}{\textbf{2}} \\
fourth thing & restarting &  \\
\hline
\end{tabular}

I was trying to be clever and not repeat myself so was trying to do two things:

  1. Set up a counter to do the counting in each cell
  2. Write a \newcommand so that I don't have to keep repeating that \multicolumn code in each cell.

I haven't been able to get it to work, and I get lots of "Illegal pream-token" or "Misplaced \omit" errors in compiling.

Here's what I tried:

In the document header:

\newcounter{DateNumber}
\setcounter{DateNumber}{22}
\newcommand{\datehead}[2]{%
    \multicolumn{1}{%
        \ifthenelse{\equal{#2}{first}}{|r|}{r|}}%
        {\textbf{#1 \theDateNumber}}\stepcounter{DateNumber}%
}

The table:

\begin{tabular}{|l|l|l|}
\hline
\datehead{}{first} & \datehead{}{} & \datehead{}{} \\
first thing & second thing & third thing \\
\hline
\datehead{}{first} & \setcounter{DateNumber}{1}\datehead{New} & \datehead{}\\
fourth thing & restarting &  \\
\hline
\end{tabular}

Any help is much appreciated!

1

You don't need \multicolumn for this, but just \hspace*{\fill}:

\documentclass{article}

\newcounter{head}
\newcommand{\head}[1]{\stepcounter{head}\hspace*{\fill}\bfseries #1 \thehead}
\newcommand{\headnew}[1]{\setcounter{head}{0}\head{#1}}

\begin{document}

\begin{tabular}{|l|l|l|}
\hline
\head{} & \head{} & \head{} \\
first thing & second thing & third thing \\
\hline
\head{} & \headnew{New} & \head{} \\
fourth thing & restarting &  \\
\hline
\end{tabular}

\end{document}

enter image description here

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.