
ifcommand
有類似from keycommand
package in 的語法嗎pgfkeys
?我想做類似下面的事情
\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}
}
答案1
這裡,我使用內部/Table/@caption
來定義如何插入標題。最初它的程式碼是空的。風格/Table/caption
將其重新定義為\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}
第一個變體
使用.try
處理程序,您可以(嘗試)/Table/@caption
在不初始化的情況下使用:
\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}
}
第二個變體
您可以將標題儲存在\TableCaption
(透過.store in
處理程序)並使用\ifdefempty
(從etoolbox
套件中):
\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}
}
答案2
也許這也可以用 來完成pgfkeys
。這是帶有密鑰的完整實作expl3
。
\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}
由於無標題表格具有tp
,因此它出現在頂部。在表格列表中,標題是較短的。
然而,我認為沒有太大的優勢
\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}