0

I'm currently figuring out how to use pgfkeys, and I'm running into the following problem. I'm trying to define a command that prints part of a table, filled with arguments provided through pgfkeys. A simplified version of my situation looks like this:

\documentclass{article}

\usepackage{pgfkeys,xparse} \pgfkeys{ /test/.is family, /test, right/.estore in=\testright, left/.estore in=\testleft, }

\NewDocumentCommand{\tablepart}{o}{ \pgfkeys{/test,#1} \testleft{} & \testright{} \ \testleft{} & \testright{} \ }

\begin{document} \begin{tabular}{rl} \tablepart[left={ABC},right={DEF}]{} \end{tabular} \end{document}

However, this setup gives me an Undefined control sequence error on \tablepart[left={ABC},right={DEF}]{}. If I remove the ampersand and \\ in the definition of \tablerow, however, the error disappears. What am I doing wrong here?

  • a table cell is a group so all definitions reset at the & – David Carlisle Aug 19 '22 at 16:34
  • Same problem: Q649183 Related problem: Q653795 – Qrrbrbirlbel Aug 19 '22 at 18:31
  • Pull the tabular usage into your \tablepart macro so that you set the values outside of the tabular environment. That way, the will all be accessoble. What are you trying to achieve? \NewDocumentCommand{\tablepart}{o}{\begingroup\pgfqkeys{/test}{#1}\begin{tabular}{rl}\testleft&\testright\\\testleft&\testright\\\end{tabular}\endgroup} – Qrrbrbirlbel Aug 19 '22 at 18:39
  • @Qrrbrbirbel the idea is that I can use the macro to insert multiple similarly structured sections into the same table. I can see how your idea would work, but that would put everything in a separate table. – Jordi Vermeulen Aug 19 '22 at 18:42
  • Then this approach: \newcommand*\addtomacro[2]{\expandafter\def\expandafter#2\expandafter{#2#1}}\NewDocumentCommand{\tablepart}{o}{\pgfqkeys{/test}{#1}\def\mymacro{}\expandafter\addtomacro\expandafter{\testleft&}{\mymacro}\expandafter\addtomacro\expandafter{\testright\\}{\mymacro}\mymacro} You basically build the row and put it (including & and \\) in a macro and use it at the end. Though, I feel there are so many table package for LaTeX that there might be already a solution for your problem. – Qrrbrbirlbel Aug 19 '22 at 18:50

1 Answers1

1

a table cell is a group so all definitions reset at the &

However you can expand the command while still in the first cell.

\documentclass{article}

\usepackage{pgfkeys,xparse} \newcommand\hmm{&}

\pgfkeys{ /test/.is family, /test, right/.estore in=\testright, left/.estore in=\testleft, }

\NewDocumentCommand{\tablerow}{o}{ \pgfkeys{/test,#1} \testleft{} \expandafter\hmm\testright{} \ }

\begin{document} \begin{tabular}{rl} \tablerow[left={ABC},right={DEF}]{} \end{tabular} \end{document}

Or for the version added later (\ExpandArgs predfined in 2022 LaTX but it could be defined for older releases)

\documentclass{article}

\usepackage{pgfkeys,xparse} \def\hmm#1#2{#1&#2\#1&#2\}

\pgfkeys{ /test/.is family, /test, right/.estore in=\testright, left/.estore in=\testleft, }

\NewDocumentCommand{\tablepart}{o}{ \pgfkeys{/test,#1} \ExpandArgs{oo}\hmm{\testleft}{\testright} }

\begin{document} \begin{tabular}{rl} \tablepart[left={ABC},right={DEF}]{} \end{tabular} \end{document}

David Carlisle
  • 757,742
  • Thank you for the answer, this does indeed solve the problem in the example I posted! Unfortunately it turns out I simplified the example too much: the real version of my \tablerow contains multiple table rows. I tried doing the same thing with the \\ as you show here with the &, but that does still give the error. I've updated the question with a more accurate example. – Jordi Vermeulen Aug 19 '22 at 18:25
  • @JordiVermeulen same basic idea, I added a working version – David Carlisle Aug 19 '22 at 19:15
  • Thanks! Is there any way to make this work on a per-row basis? I'm trying to make some sort of template for a repeating part of a table that gets its data filled using the macro with pgfkeys. I can ask a new question if that's too far removed from my original question. – Jordi Vermeulen Aug 19 '22 at 19:28
  • @JordiVermeulen new q I think (with new example) – David Carlisle Aug 19 '22 at 19:57
  • I asked a new question: https://tex.stackexchange.com/q/654604/278532. I've accepted this answer, as it does answer the question as stated here. – Jordi Vermeulen Aug 19 '22 at 21:19