2

I create two different table contents and want to switch with a newif. \AAAfalse has no effect. What do I have to do?

enter image description here

\documentclass[a4paper, landscape]{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotstableset{string type, col sep=comma, header=false}

\newif\ifAAA
\AAAtrue

\ifAAA%================================
\pgfplotstableread[header=false]{
A, A
A, A
A, A
}\mytable
\def\mycolor{red}
\else%==================================
\pgfplotstableread[header=false]{
B, B
B, B
B, B
}\mytable
\def\mycolor{blue}
\fi%=====================================

\begin{document}
\section{\color{\mycolor} AAA true :)}
\pgfplotstabletypeset[]{\mytable}

\AAAfalse
\section{\color{\mycolor} AAA false :(}
\pgfplotstabletypeset[]{\mytable}
\end{document}
3
  • You've read the table inside the conditional, changing it afterwards does nothing. Did you meant to save the setup inside a macro which itself contains the conditional?
    – Joseph Wright
    Feb 9, 2022 at 15:50
  • Yes, I think so. After \AAAfalse should come the table with the B's.
    – cis
    Feb 9, 2022 at 16:01
  • you don't test the boolean at any point after \AAAfalse so naturally it has no effect. Feb 9, 2022 at 16:19

1 Answer 1

2

When you switch the \ifAAA you have already read the table; the macro \mytable has already been defined and it will not change. One possibility is to make the loading of the table dynamic (I had to change the termination because when into a macro, the newlines are changed to space tokens and that make the inline table unreadable):


\documentclass[a4paper, landscape]{article}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\pgfplotstableset{string type, col sep=comma, header=false}

\newif\ifAAA
\AAAtrue

\def\loadtable{%
\ifAAA
\pgfplotstableread[header=false, row sep=crcr]{%
A, A \\
A, A \\
A, A \\
}\mytable
\def\mycolor{red}%
\else
\pgfplotstableread[header=false, row sep=crcr]{%
B, B \\
B, B \\
B, B \\
}\mytable
\def\mycolor{blue}%
\fi}

\begin{document}
\loadtable
\section{\color{\mycolor} AAA true :)}
\pgfplotstabletypeset[]{\mytable}

\AAAfalse
\loadtable
\section{\color{\mycolor} AAA false :(}
\pgfplotstabletypeset[]{\mytable}
\end{document}

enter image description here

Surely there are more elegant solutions, like defining two tables and switching on loading, but I got lost with \expandafters (I should seriously learn more LaTeX3).

2
  • It's only now that I've noticed that: it is not so good if you have to change the table. With such a small MWE it doesn't matter much, but in real case....
    – cis
    Feb 9, 2022 at 17:22
  • You can load the two tables with different names and then put the "if" around the \pgfplotstable command, for example...
    – Rmano
    Feb 9, 2022 at 21:38

You must log in to answer this question.

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