4

I'm trying to create a new environment with arguments that puts the content inside a multicol like this

\begin{tabular}{p{5em} p{\textwidth-3\columnsep -5em}}\\[-0.5em]
\toprule \addlinespace[1.4ex]
\textbf{NFR-1} & \textbf{Extensibility} \hfill \textbf{P1} \\ \addlinespace
\midrule[\heavyrulewidth] \addlinespace[1.4ex] 
\multicolumn{2}{p{\textwidth-2\columnsep}}{Description text} \\ \addlinespace
\midrule \addlinespace
\end{tabular}\\[1em]

which becomes correct table

When i setup the new environment as in this MWE

\documentclass{article}
\usepackage{booktabs,calc}
\usepackage{hyperref}

\newenvironment{card}[4]{
\hypertarget{#1:#2}{}
\vspace{0.75ex}
\centering
\setlength{\defaultaddspace}{1.2ex}
\begin{tabular}{p{5em} p{\textwidth-3\columnsep -5em}}\\[-0.5em]
\toprule \addlinespace[1.4ex]
\textbf{#1-#2} & \textbf{#3} \hfill \textbf{#4} \\ \addlinespace
\midrule[\heavyrulewidth] \addlinespace[1.4ex] 
\multicolumn{2}{p{\textwidth-2\columnsep}}
}{
\\ \addlinespace
\midrule \addlinespace
\end{tabular}\\ [1em]
}

\begin{document}
\begin{card}{NFR}{1}{Extensibility}{P1}
Description text.
\end{card}
\end{document}

it looks strange: enter image description here

I think it doesnt like the splitup of the multicolumn command. I've already tried \bgroup and \egroup but that only produced errors.

What can be done in a situation like this?

4
  • Don't you get an error? Your example doesn't compile for me.
    – cfr
    Jan 16, 2017 at 1:23
  • 1
    @cfr, in MWE is missed package calc.
    – Zarko
    Jan 16, 2017 at 1:35
  • @Zarko Thanks, I know. But the OP should test before posting code so that people don't have to spend time figuring out how to make the problem work before they can try to solve it :-).
    – cfr
    Jan 16, 2017 at 1:40
  • Thanks for pointing out. I just checked if the pdf gets generated and looks ok, i will better check for errors in the future ;-)
    – bbjay
    Jan 16, 2017 at 15:52

2 Answers 2

3

Something like this?

funny tabular

environ provides a way of defining new environments such that you can process the entire contents of the environment, which it captures in a macro. By default, the contents is stored in \BODY, although you can customise this if necessary to avoid clashes (see manual, prooftrees.sty includes an example of how to do this to avoid clashing with forest.sty's usage of \BODY).

\documentclass{article}
\usepackage{booktabs,environ,calc}
\usepackage{hyperref}

\NewEnviron{card}[4]{%
  \hypertarget{#1:#2}{}%
  \vspace{0.75ex}%
  \centering
  \setlength{\defaultaddspace}{1.2ex}%
  \begin{tabular}{p{5em} p{\textwidth-3\tabcolsep-5em}}\\[-0.5em]
    \toprule \addlinespace[1.4ex]
    \textbf{#1-#2} & \textbf{#3} \hfill \textbf{#4} \\ \addlinespace
    \midrule[\heavyrulewidth] \addlinespace[1.4ex]
    \multicolumn{2}{p{\textwidth-2\columnsep}}{\BODY} \\ \addlinespace
    \midrule \addlinespace
  \end{tabular}%
  \vspace{1em}% you can't use \\ here and should never use it outside tabular etc. anyhow
}

\begin{document}
\noindent
\begin{card}{NFR}{1}{Extensibility}{P1}
  Description text.
\end{card}
\end{document}
3
  • can you elaborate in answer, where is defined \BODY?
    – Zarko
    Jan 16, 2017 at 1:48
  • @Zarko You mean just this ^^?
    – cfr
    Jan 16, 2017 at 2:20
  • yes, I mean this. I suspect that BODY come from package environ but so far I didn't use it yet.
    – Zarko
    Jan 16, 2017 at 16:27
1

You want a one column tabular:

\documentclass{article}
\usepackage{booktabs,calc}
\usepackage{hyperref}
\usepackage{lipsum}

\newenvironment{card}[4]{%
  \par\hypertarget{#1:#2}{}\vspace{0.75ex}
  \centering
  \setlength{\defaultaddspace}{1.2ex}
  \begin{tabular}{p{\textwidth-2\tabcolsep}}\\[-0.5em]
  \toprule
  \addlinespace[1.4ex]
  \makebox[5em][l]{\textbf{#1-#2}}\hspace{2\tabcolsep}\textbf{#3}\hfill\textbf{#4}\\
  \addlinespace
  \midrule[\heavyrulewidth] \addlinespace[1.4ex] 
}{%
  \\ \addlinespace
  \midrule \addlinespace
  \end{tabular}
  \par\vspace{1em}
}

\begin{document}

\lipsum[2]

\begin{card}{NFR}{1}{Extensibility}{P1}
Description text.
\end{card}

\lipsum[3]

\end{document}

The spacings seem not to obey any specific rule, though.

Note that the parameter for the intercolumn spaces is not \columnsep, but rather \tabcolsep (and each column has such space at either side).

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .