
¿Existe alguna sintaxis como ifcommand
la del keycommand
paquete en pgfkeys
? quiero hacer algo como lo siguiente
\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}
}
Respuesta1
Aquí, lo uso internamente /Table/@caption
para definir cómo insertar el título. Inicialmente su código está vacío. El /Table/caption
estilo lo redefine a \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}
1ra variante
Usando el .try
controlador, puedes (intentar) usarlo /Table/@caption
sin inicialización:
\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}
}
2da variante
Puede almacenar el título en \TableCaption
(a través del .store in
controlador) y utilizar \ifdefempty
(desde etoolbox
el paquete):
\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}
}
Respuesta2
Probablemente esto también se pueda hacer con pgfkeys
. Aquí hay una implementación completa con expl3
claves.
\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}
Dado que la tabla sin título tiene tp
, aparece en la parte superior. En la lista de tablas, el título es el corto.
Sin embargo, no veo grandes ventajas sobre
\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}