# How to reuse common data elements for multiple macros?

I want to use macros to present different combinations of data elements from the same data set more than once in the same document.

For example, it is easy to summarize the elements from Data Set A in a table using a macro:

\documentclass[11pt]{article}

\newcommand\dataset[6]{%
\begin{table}
\begin{tabular}{ll}
Element 1  & #1 \\
Element 2  & #2 \\
Element 3  & #3 \\
Element 4  & #4 \\
Element 5  & #5 \\
Element 6  & #6 \\
\end{tabular}
\end{table}
}

\begin{document}

\dataset % Data Set A, one of many possible data sets in the same document
{a}
{b}
{c}
{d}
{e}
{f}

\end{document}


But now I'd like to reuse some of the same elements from Data Set A for display in a different format, but in the same document. For example:

\renewcommand\dataset[6]{%
The first element of Data Set #1, is the data set label.
The fourth element for Data Set #1 is #4.}


How can I define macros that use input parameters from the same data set, so that I only have to input data sets once? Is this the most elegant way? That is, to use \renewcommand at the point where I want to redefine the macro?

The result of this example is shown below.

In the complete application, I use \input{data.tex}. The contents of this file look like this:

\dataset % A
{Data element} % Synopsis
{Data element} % Recommendation
{Data element} % Reference
{Data element} % Risk value
{Data element} % FAI support

\dataset % B
{Data element} % Synopsis
{Data element} % Recommendation
{Data element} % Reference
{Data element} % Risk value
{Data element} % FAI support

\dataset % c
{Data element} % Synopsis
{Data element} % Recommendation
{Data element} % Reference
{Data element} % Risk value
{Data element} % FAI support

• So are you looking for a solution where you have a bunch of defs like \datasetA=a,b,c,d \datasetB=e,f,g and then using a \showtable{\datasetA} or \describe{\datasetB} you get different presentations for each dataset? – Bordaigorl Oct 2 '14 at 14:31
• That is correct. I want to present all the data sets in more than one format in the same document. One table might just summarize some of the data for all data sets, another table might show all the data from each data set. – Martin Clouthier Oct 2 '14 at 14:41
• You might also look at the xstring package. – John Kormylo Oct 2 '14 at 15:54
• Do you have a way of knowing which dataset is called what? That is, you call the datasets A, B, C,... but is that just a sequential numbering, or do you also want to reference them due to their contents, as in dataset First, Major, New,... (say)? – Werner Oct 2 '14 at 17:20
• I assume data.tex is using the definition of \dataset from the main tex file. That would mean that each dataset is printed to the pdf file already while data.tex is processed and you would have to redefine \dataset in order to save the different datasets with some names to reference them later. Can you redefine \dataset or change data.tex or are they given to you (and for some reason or other can not be changed)? – Lucas Oct 2 '14 at 17:27

You can define some infrastructure behind data sets.

After loading the data sets, you refer to each one of them by the first item (synopsis). I provide the \datasetdef command for defining various macros that use data sets.

\documentclass{article}

\makeatletter
\newcommand{\dataset}[6]{\@namedef{dataset@#1}{{#1}{#2}{#3}{#4}{#5}{#6}}}
\newcommand{\datasetdef}[2]{%
% #1 is the name of a seven argument macro
% #2 is the replacement text
\expandafter\newcommand\csname ds@\string#1\endcsname[6]{#2}%
\newcommand{#1}[1]{%
\csname ds@\string#1\expandafter\expandafter\expandafter\endcsname
\csname dataset@##1\endcsname
}%
}
\makeatother

\datasetdef{\dstable}{%
\begin{tabular}{ll}
Element 1  & #1 \\
Element 2  & #2 \\
Element 3  & #3 \\
Element 4  & #4 \\
Element 5  & #5 \\
Element 6  & #6 \\
\end{tabular}%
}

\datasetdef{\dsshowfirstandfourth}{%
The first element of Data Set #1, is the data set label.
The fourth element for Data Set #1 is #4.%
}

%%% This is like \input{data.txt}
\dataset
{A} % Synopsis
{A2} % Recommendation
{A4} % Reference
{A5} % Risk value
{A6} % FAI support
\dataset
{B} % Synopsis
{B2} % Recommendation
{B4} % Reference
{B5} % Risk value
{B6} % FAI support
\dataset
{c} % Synopsis
{c2} % Recommendation
{c4} % Reference
{c5} % Risk value
{c6} % FAI support

\begin{document}

\dstable{A} \dstable{c}

\bigskip

\dsshowfirstandfourth{A}

\dsshowfirstandfourth{B}

\end{document}


As you see, \datasetdef is similar to \newcommand, but you only have to state the name of the macro and the replacement text (with six arguments). The macro defined with \datasetdef takes a single argument, that is, the name of a data set.

The quickest idea I could come up with is the reverse of what was suggestet in the comment. You define some format macros that will format their arguments. Then you define the data set that will the apply the dirst argument to itself:

\documentclass[11pt]{article}
% some format that can be applied to any dataset with six entries
\newcommand\FormatOne[6]{%
\begin{table}
\begin{tabular}{ll}
Element 1  & #1 \\
Element 2  & #2 \\
Element 3  & #3 \\
Element 4  & #4 \\
Element 5  & #5 \\
Element 6  & #6 \\
\end{tabular}
\end{table}
}
% some other format that can be applied to any dataset with six entries
\newcommand\FormatTwo[6]{
The first element of Data Set #1, is the data set label.
The fourth element for Data Set #1 is #4.}
% A dataset with six entries. The first argument will be used as a format
% command
\newcommand\DataSetOne[1]{
\csname#1\endcsname{a}{b}{c}{d}{e}{f}}
\begin{document}
\DataSetOne{FormatOne}
\DataSetOne{FormatTwo}
\end{document}


The pdf looks like your example.

• That's very interesting. I wonder if this approach can be taken where the data sets are input from a source file. For example, in my application I have dozens of data sets defined by \dataset in a file named data.tex and I use \input{data.tex} in the main document. – Martin Clouthier Oct 2 '14 at 15:00
• How exactly are \datasets defined in your input files? – Lucas Oct 2 '14 at 15:05
• are all the datasets of known fixed length? – Bordaigorl Oct 2 '14 at 15:18
• I have updated my comments to show how each \dataset is defined. All of the data sets are defined identically. Only the data elements change. – Martin Clouthier Oct 2 '14 at 17:17