1

I have a large document in which I use and reuse pieces of data. Such a piece of data consists of a number, a short and long description and a value. For example. I want to declare all those pieces of data in advance like this:

\begin{datasnippets}
\datasnippet{licenseplate}{A licenseplate}{A valid licenseplate for Belgium}{1BEL500} 
\datasnippet{building}{An expensive house}{The address of a building costing more than 1 million}{1 Google Drive CA} 
\end{datasnippets}

I then want this environment to (a) generate a table with all datasnippets, and (b) generate a set of commands which allow me to refer to the different parts of those snippets.

So, the above snippet followed by following sentence:

I found a license plate (\licenseplateValue ). The car was located at \buildingValue . 

Will look like this.

enter image description here

Currently I implemented this as follows:

\newcounter{datasnippets}
\newcommand{\thedatasnippetcounter}{\arabic{datasnippets}}
\newenvironment{datasnippets}
{
    \small\\
    \hspace{-2cm}\begin{longtable}{p{0.4cm} p{4cm}p{3cm}p{10cm} }
  \ & \textbf{Naam} & \textbf{Waarde} & \textbf{Beschrijving} \\
    \ & \ & \ & \\
    \hline
    \newcommand{\datasnippet}[4]{%
        \refstepcounter{datasnippets}
        \label{data:##1}
        \global\expandafter\def\csname ##1\endcsname{##2}
        \global\expandafter\def\csname ##1Description\endcsname{##3}%
        \global\expandafter\def\csname ##1Value\endcsname{##4}%     
      \global\expandafter\def\csname ##1Full\endcsname{##3 (waarde: ##4)}%      

        \hspace{-2cm}
        \begin{tabular}{p{0.2cm}p{4cm}p{3cm}p{10cm} }
        \thedatasnippetcounter . & ##2 & ##4 & ##3 \\
        \ & \ & \ & \\
        \end{tabular}
    }
}
{
    \end{longtable}
}

The result somewhat behaves the way I want it to, but the table behaves very badly when it gets too long as it doesn't break over pages. Also, the fact that I use tabulars inside a tabular is a poor mans solution, since I didn't get it to work without the inner tabular (and I have no idea why - the outer tabular is open and only closed at the very end?).

So my question is: how do I get this working without the second tabular and how do I get the table to break nicely over multiple pages?

Thanks,

1 Answer 1

2

It's not clear what the inner tabular is for:

\documentclass{article}

\usepackage[
  left=1cm,
  right=1cm,
  textheight=3cm, % just to make the example smaller
]{geometry}

\usepackage{longtable}

\newcounter{datasnippets}
\newcommand{\datasnippet}[4]{%
  \refstepcounter{datasnippets}\label{data:#1}%
  \global\expandafter\def\csname #1\endcsname{#2}%
  \global\expandafter\def\csname #1Description\endcsname{#3}%
  \global\expandafter\def\csname #1Value\endcsname{#4}%
  \global\expandafter\def\csname #1Full\endcsname{#3 (waarde: #4)}%
  \thedatasnippets. & #2 & #4 & #3 \\
}
\newenvironment{datasnippets}
 {\par
  \small
  \begin{longtable}{p{0.4cm}p{4cm}p{3cm}p{10cm}}
  & \textbf{Naam} & \textbf{Waarde} & \textbf{Beschrijving} \\
  \hline
  \endhead
 }
 {%
  \end{longtable}
 }

\begin{document}

\begin{datasnippets}
\datasnippet{licenseplate}
  {A licenseplate}
  {A valid licenseplate for Belgium}
  {1BEL500}
\datasnippet{building}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building2}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building3}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA}
\datasnippet{building4}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building5}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building6}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building7}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\end{datasnippets}

I found a license plate (\licenseplateValue). The car was located at \buildingValue. 

\end{document}

enter image description here

Of course you need to set the lengths so that you don't overflow the text width.

Here's a different implementation:

\documentclass{article}

\usepackage{longtable,array}

\newcounter{datasnippets}
\newcommand{\datasnippet}[4]{%
  \refstepcounter{datasnippets}\label{data:#1}%
  \global\expandafter\def\csname #1\endcsname{#2}%
  \global\expandafter\def\csname #1Description\endcsname{#3}%
  \global\expandafter\def\csname #1Value\endcsname{#4}%
  \global\expandafter\def\csname #1Full\endcsname{#3 (waarde: #4)}%
  \thedatasnippets. & #2 & #4 & #3 \\
}
\newenvironment{datasnippets}
 {\par
  \small
  \begin{longtable}{
    p{0.4cm}
    p{4cm}
    p{3cm}
    >{\raggedright\arraybackslash}p{\dimexpr\textwidth-8\tabcolsep-0.4cm-4cm-3cm\relax}
  }
  & \textbf{Naam} & \textbf{Waarde} & \textbf{Beschrijving} \\
  \hline
  \endhead
 }
 {%
  \end{longtable}
 }

\begin{document}

\begin{datasnippets}
\datasnippet{licenseplate}
  {A licenseplate}
  {A valid licenseplate for Belgium}
  {1BEL500}
\datasnippet{building}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building2}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building3}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA}
\datasnippet{building4}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building5}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building6}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\datasnippet{building7}
  {An expensive house}
  {The address of a building costing more than 1 million}
  {1 Google Drive CA} 
\end{datasnippets}

I found a license plate (\licenseplateValue). The car was located at \buildingValue. 

\end{document}

enter image description here

1
  • There is no use whatsoever for the inner tabular, but for some reason, my code didn't work without it (none of the commands were known). I would think, since my outer and inner tabular had the same number of columns, I would be able to simply leave out the begin and end tabular lines of the inner tabular, and have basically the same result as I had, but this isn't the case.
    – Yves V.
    Aug 2, 2016 at 19:24

You must log in to answer this question.

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