无自动编号的新环境类型

无自动编号的新环境类型

我正在尝试使用tabu构建块来定义一个新环境。使用它的原因tabu是它可以很好地跨页面拆分长表。我希望得到一些帮助来完成以下工作:

  1. \listofinterfaces创建一种出现在而不是中的新类型的环境\listoftables
  2. 标题应改为Interface X: textTable X: text
  3. 不需要自动编号,因为我更喜欢使用每个表提供的自己的编号(它目前是的第 3 个参数ryetable);这也意味着当我引用新环境时,会显示提供的数字而不是某些自动计数器。

我知道你可能需要一些严肃的乳胶巫术来实现这一点,但即使是一些正确方向的指示也会有所帮助。


\documentclass{article}

\usepackage{array, booktabs, longtable, tabu}

% Usage: {}{<label>}{<ifNum>}{<caption>}
\newenvironment{ryetable}[4]{%
  \gdef\tmpheaders{#1} % Not currently used
  \gdef\tmplabel{\label{#2}} % Save label for later use
  \gdef\tmpifnum{#3} % Save interface number
  \gdef\tmpcaption{\caption{#4}} % Save caption for later use
  \renewcommand{\arraystretch}{1.3} % More space between rows
  \begin{longtabu} to \textwidth {@{} X[1,l] X[1,l] X[1,l] >{\raggedright}X[1,l] @{}}
    \\\toprule\rowfont\bfseries
    Header1 & Header2 & Header3 & Description \\
    \midrule
  \endfirsthead
    \multicolumn{4}{@{}c@{}}{\small\textit{(continued)}}\\
    \toprule\rowfont\bfseries
    Header1 & Header2 & Header3 & Description \\
    \midrule
  \endhead
    \bottomrule
    \multicolumn{4}{@{}r@{}}{\small\textit{(continues)}}\\
  \endfoot
    \bottomrule
    \tmpcaption
    \tmplabel
  \endlastfoot
}{%
  \end{longtabu}
}

\begin{document}
\listoftables
%\listofinterfaces %TODO
\section{MySection}
For a simple table see Table~\ref{tab:somelabel} and for a nifty new environment check Interface~\ref{if:myinterface}.

%TODO: 31 here should be the environment's number
\begin{ryetable}{}{if:myinterface}{31}{This is my pretty interface.}
a & b & c & d\\
e & f & g & h\\
i & j & k & l\\
m & n & o & p\\
q & r & s & t\\
u & v & w & x\\
y & z & -- & --\\
\end{ryetable}


% Some other table to show on list
\begin{table}[h]
  \centering
  \begin{tabular}{c|c}
    a&b\\
    c&d\\
  \end{tabular}
  \caption{Caption of tabular table.}
  \label{tab:somelabel}
\end{table}
\end{document}

在此处输入图片描述

答案1

可能的解决方案的路径:

  • 使用\caption[]{...}而不是\caption{...}-->表格列表中没有条目
  • \ryetable重新定义\tablename为内Interface,它是安全的,因为它在一个组内。
  • 还重新定义\thetable(负责将“表”编号打印为用户提供的数字,对于其余环境也是安全的,因为重新定义发生在组内。
  • 定义一个粗略的\listofinterfaces命令,使用新的 toc-type int(接口)并用于@starttoc{int}读取环境\addcontentsline{int}{subsection}{...}内命令提供的 toc 条目。ryetable
  • 将计数器减table一,因为ryetable它会自动增加,因此对table环境的后续调用将会得到错误的数字。

代码

\documentclass{article}

\usepackage{array, booktabs, longtable, tabu}

\newcommand{\ListOfInterfacesName}{List of Interfaces}%
\newcommand{\InterfaceName}{Interface}%

\makeatletter%
\newcommand{\listofinterfaces}{%
\section*{\ListOfInterfacesName}%
\@starttoc{int}  % New toc with extension *.int%
\clearpage%
}%
\makeatother%

% Usage: {}{<label>}{<ifNum>}{<caption>}
\newenvironment{ryetable}[4]{%
\renewcommand{\tablename}{\InterfaceName}%
\renewcommand{\thetable}{#3} % Explicitly set the number to the given argument
\addtocounter{table}{-1}%
\addcontentsline{int}{subsection}{\protect{\numberline{#3}{#4}}}%   Define an entry subsection like... (as normal tables)
  \gdef\tmpheaders{#1} % Not currently used
  \gdef\tmplabel{\label{#2}} % Save label for later use
  \gdef\tmpifnum{#3} % Save interface number
  \gdef\tmpcaption{\caption[]{#4}} % Save caption for later use ---> drop the entry to the table list
  \renewcommand{\arraystretch}{1.3} % More space between rows
  \begin{longtabu} to \textwidth {@{} X[1,l] X[1,l] X[1,l] >{\raggedright}X[1,l] @{}}
    \\\toprule\rowfont\bfseries
    Header1 & Header2 & Header3 & Description \\
    \midrule
  \endfirsthead
    \multicolumn{4}{@{}c@{}}{\small\textit{(continued)}}\\
    \toprule\rowfont\bfseries
    Header1 & Header2 & Header3 & Description \\
    \midrule
  \endhead
    \bottomrule
    \multicolumn{4}{@{}r@{}}{\small\textit{(continues)}}\\
  \endfoot
    \bottomrule
    \tmpcaption
    \tmplabel
  \endlastfoot
}{%
  \end{longtabu}
}

\begin{document}
\listoftables
\listofinterfaces %TODO
\section{MySection}
For a simple table see Table~\ref{tab:somelabel} and for a nifty new environment check Interface~\ref{if:myinterface}.

%TODO: 31 here should be the environment's number
\begin{ryetable}{}{if:myinterface}{31}{This is my pretty interface.}
a & b & c & d\\
e & f & g & h\\
i & j & k & l\\
m & n & o & p\\
q & r & s & t\\
u & v & w & x\\
y & z & -- & --\\
\end{ryetable}


% Some other table to show on list
\begin{table}[h]
  \centering
  \begin{tabular}{c|c}
    a&b\\
    c&d\\
  \end{tabular}
  \caption{Caption of tabular table.}
  \label{tab:somelabel}
\end{table}
\end{document} 

在此处输入图片描述 在此处输入图片描述

相关内容