1

I'm doing a project where I need to copy a table several times throughout a document. The basic structure for the table is the same, however, some of the input needs to change. I was wondering if it is possible to define a basic layout (or some kind of template) where I need only to fill in the variable data and not setup the whole table once again.

My problem is that there's a big chance of human error when copy pasting tables, to change them later on.

Let's take an example of what I would like. First here is a table definition:

\begin{table}[h]
\makebox[\linewidth]
{
    \begin{tabular}{|l|l|l|}
    \hline
    [static]            & [static]  & [static]  \\ \hline
    [static]            & [DYNAMIC] & [DYNAMIC] \\ \hline
    [static]            & [DYNAMIC] & [DYNAMIC] \\ \hline
    [static]            & [DYNAMIC] & [DYNAMIC] \\ \hline
    \end{tabular}
}
\caption{[DYNAMIC}
\label{[DYNAMIC]}
\end{table}

What I'm searching for is a way of replicating the table where the things marked with [static] should not be changed whenever I replicate the table, however, the things that are marked with [DYNAMIC] should be changed in each table. So what I'm looking for is some kind of way to define the base layout that makes it easy to "paste in" the data that needs to be changed.

I have been searching on google for this, but with no luck, so now I'm wondering if this is even possible with latex, and if so what is the correct way to do this?

Thank you :)

  • 3
    Welcome to the site! A good question :) We've had a few like it before -- have a search for pgfplotstable – cmhughes Dec 9 '14 at 22:03
  • 2
    the caption and label are easy enough to add ad hoc, so what you really want is a way to replace the cells marked "DYNAMIC". if all the tables are exactly the same size and configuration, and there aren't too many variable cells, why not just assign each such cell a unique and memorable command name, say \mycellbC for the third cell across in the second row down. you can bundle up the tabular as a single command. then you can assign each cell an initial default, and reassign values just before triggering the command for the tabular block. – barbara beeton Dec 9 '14 at 22:09
2

You can use a key-value system, for instance expl3.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\archietable}{m}
 {
  \begin{table}[htp]
  \centering
  \keys_set:nn { archie/table } { #1 }
  \begin{tabular}{|l|l|l|}
  \hline
  STATIC & STATIC & STATIC \\ \hline
  STATIC & \l_archie_table_XA_tl & \l_archie_table_YA_tl \\ \hline
  STATIC & \l_archie_table_XB_tl & \l_archie_table_YB_tl \\ \hline
  STATIC & \l_archie_table_XC_tl & \l_archie_table_YC_tl \\ \hline
  STATIC & \l_archie_table_XD_tl & \l_archie_table_YD_tl \\ \hline
  \end{tabular}
  \caption{\l_archie_table_caption_tl}
  \label{\l_archie_table_label_tl}
  \end{table}
 }

\keys_define:nn { archie/table }
 {
  XA      .tl_set:N = \l_archie_table_XA_tl,
  XB      .tl_set:N = \l_archie_table_XB_tl,
  XC      .tl_set:N = \l_archie_table_XC_tl,
  XD      .tl_set:N = \l_archie_table_XD_tl,
  YA      .tl_set:N = \l_archie_table_YA_tl,
  YB      .tl_set:N = \l_archie_table_YB_tl,
  YC      .tl_set:N = \l_archie_table_YC_tl,
  YD      .tl_set:N = \l_archie_table_YD_tl,
  caption .tl_set:N = \l_archie_table_caption_tl,
  label   .tl_set:N = \l_archie_table_label_tl,
 }

\ExplSyntaxOff

\begin{document}

\archietable{
  XA = foo,
  XB = bar,
  XC = baz,
  XD = foobar,
  YA = gnu,
  YB = gnat,
  YC = gnocco,
  YD = whatever,
  caption=First caption,
  label=flabel,
}

\archietable{
  XA = 1,
  XB = 2,
  XC = 3,
  XD = 4,
  YA = 5,
  YB = 6,
  YC = 7,
  YD = 8,
  caption=Second caption,
  label=slabel,
}

\end{document}

Change the STATIC values; remember that spaces in the text in the definition of \archietable should be inserted as ~.

The order in which you specify the keys is immaterial.

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.