ifcommand-like syntax for pgfkeys
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}
}
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}