Graphics, Figures & TablesCreate table with variable content

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
redlioness
Posts: 1
Joined: Mon Nov 13, 2023 9:43 am

Create table with variable content

Post by redlioness »

I want to create a larger document with a variable number of sections that are categorised in different categories, where each section is a different latex document. Aside from the TOC, I want to create a table (eg tabular) where one column is the name of the section and the other is the category. I want it to automatically update depending on the numher of latex files. Example: different activity categories such as sport, eating, relaxation. I then have 5 files (named activity1.tex, activity2.tex, activity3.tex, activity4.tex, activity.tex):
1) watching the new hungergames movie
category relaxation
2) going walking in a park
category sport
3) going to McDonalds
category eating
4)doing an escape room
category relaxation
5) going to a fancy restaurant
category eating

In my main file, if necessary, I can create a variable with the number of files that I fill in myself. What I would like is a table that states in one column the title of the activity and in the other the category. But I want it to be filled out automatically depending on the number of files (as long as I state the activity name and category as a variable in the file itself for example)
Does anyone know how to do that? I thought maybe with a forloop but I cannot get it to work

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Create table with variable content

Post by rais »

You can split your project in as many files as you want, but I wouldn't make anything depending on that. Here's an idea using a TOC-like approach:

Code: Select all

\documentclass{article}
\usepackage{blindtext}
\newlength\activitywidth % the width for activity within the list of activities (LOA)
\newlength\categoryskip % the horizontal skip between activity and category in LOA
\newcommand*\actlistext{act}% the extension for LOA file;
% since `loa' may be in use already (as in list of algorithms), I just used the first
% 3 characters from `activity'
\AtBeginDocument{% delayed code:
% \linewidth and similar lengths may not be valid in preamble
  \setlength\activitywidth{0.7\linewidth}%
  \setlength\categoryskip{2em}%
}% delayed
\newcommand*\activity[3][]{%
  \ifx\relax#1\relax % if #1 is empty or \relax
    \section{#2}%
    \addtocontents{\actlistext}{\protect\activitylistentry{#2}{#3}}%
  \else
    \section[#1]{#2}%
    \addtocontents{\actlistext}{\protect\activitylistentry{#1}{#3}}%
  \fi
}%\activity[entry for TOC]{activity}{category}
\newcommand*\activitylistentry[2]{%
  \noindent\parbox[t]{\activitywidth}{\raggedleft#1}\hfil
  \parbox[t]{\dimexpr\linewidth-\activitywidth-\categoryskip\relax}{\raggedright#2}\par
}%\activitylistentry{activity}{category}
\makeatletter
  \newcommand*\listofactivities{%
    \section*{Activities}
    \markboth{Activities}{Activities}%
    \activitylistentry{\textbf{Activity}}{\textbf{Category}}
    \@starttoc{\actlistext}%
  }%\listofactivities
\makeatother
\begin{document}
\tableofcontents
\listofactivities
\activity{watching the new hungergames movie}{relaxation}
\blindtext
\activity{going walking in a park}{sport}
\activity[visiting the Golden Seagull Inn]{going to McDonalds}{eating}
\activity{doing an escape room}{relaxation}
\activity{going to a fancy restaurant}{eating}
\end{document}
KR
Rainer
Post Reply