3

I wonder if I can define something similar to the \newcommand that places the values inside a pre-defined table (tabular) template.

table layout

If you see the table above, imagine you would duplicate that many times (for instance by using a newly defined command \workpackage). The input arguments for that command would be:

\workpackage {2}{
\begin{itemize}......
}{none}

The layout itself is a table. Is it a good idea to put such a complex layout into a new command as kind of a template?

This example does not work :(

\newcommand{\wp}[3]{
    \begin{tabular}{ l l }
      Effort & #1 \\
      Content & #2 \\
      Pre-Requisites & #3 \\
    \end{tabular}
}

Here's my final solution, which is inspired by the answers given below:

\usepackage{tabularx}
\usepackage{xkeyval}

% define the key (arguments)
\makeatletter
\define@key{wpkeys}{title}{%
  \def\wptitle{#1}
}
\define@key{wpkeys}{number}{%
  \def\wpnumber{#1}
}
\define@key{wpkeys}{weeks}{%
  \def\wpweeks{#1 person weeks}
}
\define@key{wpkeys}{results}{%
  \def\wpresults{#1}
}
\makeatother
% end of key definition

% new command for a work package
\newcommand{\workpackage}[2][]{%
    \setkeys{wpkeys}{#1}%

    \subsection*{WP\wpnumber: \wptitle}
    \begin{tabularx}{\linewidth}{|p{3cm}|X|}
        \hline
        Effort & \wpweeks \tabularnewline
        \hline
        Content & #2 \tabularnewline
        \hline
        Expected results & \wpresults \tabularnewline
        \hline
    \end{tabularx}
}
% end of command definition

Used like this:

\workpackage[number=1, title={Project Management \& Reporting}, weeks=3,results={MS1}]{
    \begin{itemize} 
        \item foo 
        \item baa
    \end{itemize}
}
  • 1
    Remember that a \newcommand only takes up to 9 arguments. If your table requires more entries than that, something more clever is required. – Steven B. Segletes Mar 23 '16 at 12:28
  • There will be less than 9 parameters. But just curious what something else more clever would be? – Matthias Mar 23 '16 at 12:33
  • Well for things with more than nine arguments, you could pass the data in a single argument as a space or comma separated list, and then have the macro parse the data accordingly. See tex.stackexchange.com/questions/2132/… – Steven B. Segletes Mar 23 '16 at 12:37
  • I have done this for my worksheet headers and using a keyvalue-syntax to make it easier. A comma separated list is easier, but requires to remember which position stands for which entry in the table then – user31729 Mar 23 '16 at 12:39
  • Hmm, a list is a good idea though. But nevertheless, I haven't found an easily understandable example how to iterate through arguments. – Matthias Mar 23 '16 at 12:43
3

Here's a keyvalue interface approach.

Define 'arbitrary' keys for a key family, say wpkeys with \define@key and assign the key-value to a macro that has a similar name like the key (easier to remember). Those keys can be given as an optional argument to \workpackage then.

The real content is given as the first mandatory argument, for example.

With

\setkeys{wpkeys}{prerequisites=none,leftheader={Effort},#1}%

it is possible to preset some keys that should some values if they are not explicitly given.

Please note that due to the shortness of my example this will leave the content of \leftheader etc. defined even for the next usage of the \workpackage macro.

\documentclass{article}

\usepackage{tabularx}
\usepackage{xkeyval}

\makeatletter

\define@key{wpkeys}{leftheader}{%
  \def\leftheader{#1}
}

\define@key{wpkeys}{rightheader}{%
  \def\rightheader{#1}
}

\define@key{wpkeys}{prerequisites}{%
  \def\prerequisites{#1}
}




\makeatother


\newcommand{\workpackage}[2][]{%
\setkeys{wpkeys}{prerequisites=none,leftheader={Effort},#1}%
\begin{tabularx}{\linewidth}{|p{3cm}|X|}
\hline
\leftheader & \rightheader \tabularnewline
\hline
Content & #2 \tabularnewline
\hline
Pre-requisites & \prerequisites \tabularnewline
\hline
\end{tabularx}
}

\begin{document}

\workpackage[rightheader={2 person weeks}]{\begin{itemize} \item foo \item foobar \end{itemize}}


\workpackage[rightheader={5 person weeks},prerequisites={a lot}]{\begin{itemize} \item foo \item foobar \end{itemize}}

\end{document}

I leave the table design to the O.P.

enter image description here

Update A little bit nicer/easier to use:

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tabularx}
\usepackage{xkeyval}

\makeatletter

\define@key{wpkeys}{leftheader}{%
  \def\leftheader{#1}
}

\define@key{wpkeys}{rightheader}{%
  \def\rightheader{#1}
}

\define@key{wpkeys}{prerequisites}{%
  \def\prerequisites{#1}
}

\define@key{wpkeys}{contentname}{%
  \def\contentcolumnheadername{#1}
}

\define@key{wpkeys}{prerequisitesname}{%
  \def\prerequisitescolumnheadername{#1}
}

\define@key{wpkeys}{contentheaderstyle}{%
  \def\contentheaderstyle{#1}%
}

\define@key{wpkeys}{prerequisitesheaderstyle}{%
  \def\prerequisitesheaderstyle{#1}%
}

\define@key{wpkeys}{leftheaderstyle}{%
  \def\leftheaderstyle{#1}%
}

\define@key{wpkeys}{rightheaderstyle}{%
  \def\rightheaderstyle{#1}%
}


% Tabular lengths etc. 

\newlength\wp@leftcolumnwidth
\setlength{\wp@leftcolumnwidth}{3cm} % A default value

\define@key{wpkeys}{leftcolumnwidth}{%
  \setlength{\wp@leftcolumnwidth}{#1}%
}

\define@key{wpkeys}{arraystretch}{%
  \renewcommand{\arraystretch}{#1}%
}


\presetkeys{wpkeys}{%
  arraystretch=1.5,
  contentheaderstyle={\textbf},
  prerequisitesheaderstyle={\textit},
  rightheaderstyle={\large\bfseries\textcolor{blue}},
  leftheaderstyle={\large\bfseries\textcolor{red}},
}{}



\newcommand{\workpackage}[2][]{%
  \begingroup
  \setkeys{wpkeys}{contentname={Content},prerequisitesname={Pre - Requisites},prerequisites=none,leftheader={Effort},#1}%
  \begin{tabularx}{\linewidth}{|p{\wp@leftcolumnwidth}|X|}
    \hline
    \leftheaderstyle{\leftheader} & \rightheaderstyle{\rightheader} \tabularnewline
    \hline
    \contentheaderstyle{\contentcolumnheadername} & #2 \tabularnewline
    \hline
    \prerequisitesheaderstyle{\prerequisitescolumnheadername} & \topsep=0pt\prerequisites \tabularnewline
    \hline
  \end{tabularx}
  \endgroup
}

\makeatother


\begin{document}

\workpackage[rightheader={2 person weeks}]{\begin{itemize} \item foo \item foobar \end{itemize}}


\workpackage[rightheader={5 person weeks},prerequisites={\begin{itemize} \item A nice list \item that ends \item after three items\end{itemize}}]{\begin{itemize} \item foo \item foobar \end{itemize}}

\end{document}

enter image description here

  • cool example. but I don't understand two points. First: the double brackets in the newcommand line [2][] and the #1 in the end of the setkeys line. Why would you then invoke the command giving some arguments in the brackets and some in braces? – Matthias Mar 23 '16 at 13:07
  • Ok, understood most of it. There is just no clear rule when to use keys (in brackets) and just the arguments (in curly braces) otherwise. If the pre-requisites are also a list of bullets points, then you would rather use them as an argument and not as keys. – Matthias Mar 23 '16 at 13:39
  • @Matthias: The [...] is the optional argument. You can just omit it and then default settings for the key values come into action. (those ones you can set with \setkeys{...}{...,#1}. You see, that #1 occurs there -- i.e. the keys specified in [...] come last, overriding the default ones. – user31729 Mar 23 '16 at 13:46
  • Ah, thanks for clarifying. Anyway, the itemize doesn't work for me. It says that maybe an \item is missing although it's there. – Matthias Mar 23 '16 at 13:51
  • 1
    @Matthias: You can use prerequisites={\begin{itemize} \item ...\end{itemize}} as well. Just use {....} around the whole key value to be on the safe side! Und schönen Gruß nach Österreich! – user31729 Mar 23 '16 at 13:53

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.