0

This should a simple question with a simple answer but I couldn't find something related to this online (being also a TeX newbie...).

I am trying to automate table generation so that I can avoid copy pasting table code over and over again. I am using the pgfplotstable package to read data and generate table code. The table columns (names, formatting, etc.) is the same; what changes is the data. Right now I copy/paste the following every time I need this table:

\begin{table}[H]
\renewcommand\tabcolsep{2.5pt}
    \centering
    \pgfplotstabletypeset[
        header=has colnames,
        every head row/.style={before row=\toprule,after row=\midrule},
        every last row/.style={after row=\bottomrule},
        every nth row={6}{before row=\midrule},
        columns/0/.style={column name=$x$, int detect},
        columns/1/.style={column name=$y$, string type},
        columns/2/.style={column name={$z$, fixed, precision=1},
        ]{some_data.csv}
    \caption{some_caption}
    \label{tab:some_label}
\end{table}

Is there a way to avoid typing this whole thing and create some sort of function where I would only specify the data to read from?

Thanks!

1 Answer 1

2

You can put everything in one macro command (\TypesetTabla). For this example it uses 3 parameters: the name of the data file, the caption's text and the label.

Here the command is invoked twice using two different datasets.

\TypesetTabla{<data file>}{<caption text>}{<unique label>}

a

This is the code used.

\documentclass[12pt,a4paper]{article}

\usepackage{booktabs} 
\usepackage{pgfplotstable} 

\begin{filecontents*}[overwrite]{some_dataI.dat}
    column1 column2 column3
    1 xx 3.1
    11 3 23.2 
    12 4 39.3 
    1 6  3.4 
    2 7 6.5 
    14 8 16.6 
    1123 cc 3.1
    11 0 23.2 
    16 2 39.3 
    1 bnm 3.4 
    2 5 6.5 
    14 ert 16.6 
\end{filecontents*}

\begin{filecontents*}[overwrite]{some_dataII.dat}
    column1 column2 column3
    5 mmm 7.1
    11 3 4.2 
    12 srt 39.3 
    6 6  3.4 
    2 7 6.5 
    234 wert 16.6 
    45 cc 3.1
    11 000 8.2 
    126 2 39.3 
    32 exc 12.4 
    2 5 6.5 
    144  ert 1100.6 
    345 nnn 39.3 
    12 wert 12.4 
    234 5 6.5 
    77  hyui 1100.6 
\end{filecontents*}

\newcommand{\TypesetTabla}[3]{%
    \begin{table}[ht!]
        \renewcommand\tabcolsep{2.5pt}
        \centering
        \pgfplotstableset{columns={column1,column2,column3}}
        \pgfplotstabletypeset[
        header=has colnames,
        every head row/.style={before row=\toprule,after row=\midrule},
        every last row/.style={after row=\bottomrule},
        every nth row={6}{before row=\midrule},
        columns/column1/.style={column name=$x$,int detect},
        columns/column2/.style={column name=$y$, string type},
        columns/column3/.style={column name=$z$,fixed, precision=1},        
        ]{#1}
        \caption{#2}
        \label{#3}
    \end{table}
}
\begin{document}
    
\TypesetTabla{some_dataI.dat}{some caption I}{tab:some_label}

\TypesetTabla{some_dataII.dat}{some other caption II}{tab:some_label2}
    
\end{document}
1
  • Exactly what I needed; hopefully this helps others as well. Thanks!
    – gplt
    Feb 19, 2022 at 1:03

You must log in to answer this question.

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