# \pgfplotstablegetelem in \section does not work

I read in some dates with \pgfplotstableread and want to use this dates. The idea is, I want to be able to change its values without having to change my LaTeX code.

The following MWE will not compile (on Win 8, MiKTeX 2.9), if I uncomment the section command. I think my macro dateFROMtableis not robust enough for section.

\documentclass{article}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usetikzlibrary{calendar}

\newcommand{\julianTOdate}[1]{%
\pgfcalendarjuliantodate{#1}{\myyear}{\mymonth}{\myday}%
\edef\outdate{\myyear-\mymonth-\myday}\outdate}

\newcommand{\dateFROMtable}[2]{%
\pgfplotstablegetelem{0}{#2}\of{#1}%
\julianTOdate{\pgfplotsretval}}

\begin{filecontents}{table.dat}
first.date last.date
2457039 2457041
\end{filecontents}

\begin{document}%
\pgfplotstableread{table.dat}{\mytable}
This is a JulianTOdate test \julianTOdate{2457040}. The first date is \dateFROMtable{\mytable}{first.date}. The last date is \dateFROMtable{\mytable}{last.date}.
\section{This is a JulianTOdate test \julianTOdate{2457040}} %% works
%\section{The last date is \dateFROMtable{\mytable}{last.date}.} %% does not work
%\section{{\pgfplotstablegetelem{0}{last.date}\of{\mytable}}\pgfplotsretval}  %% does not work
\pgfplotstablegetelem{0}{last.date}\of{\mytable}
\section{The last value is \pgfplotsretval.}  %% works
\end{document}

How can I fix it? I want to be able to use this macro multiple times in my document and in various environments (pgfplots etc.).

• The last version you have is the correct way to go. You can't protect everything from \section (or not without a hack which always brings more trouble than what it solves for \section) – percusse Mar 18 '15 at 19:36

## 1 Answer

I went along as percusse suggested. I saved the value in a macro (its name given by the new third argument). As it is already expanded, it can be used in sections etc.

\documentclass{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usetikzlibrary{calendar}

\newcommand{\dateFROMtableTOmacro}[3]{\pgfplotstablegetelem{0}{#2}\of{#1}%
\pgfcalendarjuliantodate{\pgfplotsretval}{\myyear}{\mymonth}{\myday}%
\edef#3{\myyear-\mymonth-\myday}}

\pgfplotstableread{
first.date last.date
2457039 2457041
}\mytable

\begin{document}%
\dateFROMtableTOmacro{\mytable}{first.date}{\myFirstDate}
The first date is \myFirstDate.
\section{The first date is \myFirstDate.} %% works
\end{document}