6

Is there any syntax like ifcommand from keycommand package in pgfkeys? I want to do something like the following

\pgfkeys{
    /Table/.is family,
    /Table,
        caption/.ecode = ??? 
}

\newenvironment{Table}[1][]{
    \pgfkeys{/Table, #1}
    \begin{table}
        \TableCaption % expand to \caption{/Table/caption} if /Table/caption is set
                      % expand to nothing otherwise
}{
    \end{table}
}
2
  • 1
    instead of an if switch you can set a key /.code and use it regardless of set or empty.
    – percusse
    Jan 21, 2017 at 13:21
  • 1
    @percusse \caption command in /.code causes an error for me (\caption outside of the floating environment), because \pgfkeys{/Table, #1} command is called before \begin{table} as I can see. Jan 21, 2017 at 13:25

2 Answers 2

4

Here, I use internally /Table/@caption to define how to insert the caption. Initially its code is empty. The /Table/caption style redefines it to \caption{#1}.

\documentclass{article}
\usepackage{pgfkeys}

\pgfkeys{/Table/.is family,/Table}
\def\Tablekeys#1{\pgfkeys{/Table,#1}}
\Tablekeys{
  @caption/.code={},
  caption/.style={@caption/.code={\caption{#1}}},
}

\newenvironment{Table}[1][]{
  \Tablekeys{#1}
  \begin{table}[h]
    \Tablekeys{@caption}
    \centering
  }{%
  \end{table}
}

\begin{document}

\begin{Table}
  \fbox{Content of table 1}
\end{Table}

\begin{Table}[caption=Table 2]
  \fbox{Content of table 2}
\end{Table}

\end{document}

1st Variant

Using the .try handler, you can (try to) use /Table/@caption without initialization:

\pgfkeys{/Table/.is family,/Table}
\def\Tablekeys#1{\pgfkeys{/Table,#1}}
\Tablekeys{
  caption/.style={@caption/.code={\caption{#1}}}
}

\newenvironment{Table}[1][]{
  \Tablekeys{#1}
  \begin{table}[h]
    \Tablekeys{@caption/.try}
    \centering
  }{%
  \end{table}
}

2nd Variant

You may store the caption in \TableCaption (via the .store in handler) and use \ifdefempty (from etoolbox package):

\usepackage{etoolbox}

\pgfkeys{/Table/.is family,/Table}
\def\Tablekeys#1{\pgfkeys{/Table,#1}}
\Tablekeys{
  caption/.store in=\TableCaption,
  % default caption is empty
  caption=,
}

\newenvironment{Table}[1][]{
  \Tablekeys{#1}
  \begin{table}[h]
    \ifdefempty{\TableCaption}{}{\caption{\TableCaption}}
    \centering
  }{%
  \end{table}
}
0
4

Probably this can be done also with pgfkeys. Here's a full implementation with expl3 keys.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\keys_define:nn { constructor/table }
 {
  caption   .tl_set:N  = \l_constructor_table_caption_tl,
  short     .tl_set:N  = \l_constructor_table_shortcaption_tl,
  label     .tl_set:N  = \l_constructor_table_label_tl,
  alignment .tl_set:N  = \l_constructor_table_alignment_tl,
  alignment .initial:n = \centering,
  specifier .tl_set:N  = \l_constructor_table_specifier_tl,
  specifier .initial:n = htp,
}
\NewDocumentEnvironment{Table}{O{}}
 {
  \keys_set:nn { constructor/table } { #1 }
  \use:x { \exp_not:N \begin{table}[\l_constructor_table_specifier_tl] }
  \tl_if_empty:NF \l_constructor_table_caption_tl
   {
    \tl_if_empty:NTF \l_constructor_table_shortcaption_tl
     {
      \caption
       {
        \l_constructor_table_caption_tl
       }
     }
     {
      \caption
       [
        \l_constructor_table_shortcaption_tl
       ]
       {
        \l_constructor_table_caption_tl
       }
     }
   }
  \tl_if_empty:NF \l_constructor_table_label_tl
   {
    \label{\l_constructor_table_label_tl}
   }
  \l_constructor_table_alignment_tl
 }
 {\end{table}}
\ExplSyntaxOff

\begin{document}

\listoftables

\begin{Table}[
  specifier=tp,
]
  \fbox{Content of noncaptioned table}
\end{Table}

\begin{Table}[
  caption=caption text,
  short=short,
  label=foo,
  alignment=\raggedright,
]
  \fbox{A table with a caption}
\end{Table}

With a reference to table~\ref{foo}.

\end{document}

Since the noncaptioned table has tp, it appears at the top. In the list of tables, the caption is the short one.

However, I see no great advantages over

\begin{table}[tp]
  \fbox{Content of noncaptioned table}
\end{table}

\begin{table}
  \caption[short]{caption text}\label{foo}
  \raggedright
  \fbox{A table with a caption}
\end{table}

enter image description here

You must log in to answer this question.

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