第一种变体

第一种变体

有没有类似ifcommandfrom keycommandpackage 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

这里我使用 internally/Table/@caption来定义如何插入标题。最初它的代码是空的。style/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}

在此处输入图片描述

相关内容