(重新)在部分内设置当前部分名称

(重新)在部分内设置当前部分名称

我正在尝试从该部分内的某个地方重置 currents 部分,但在\section。根据 LaTeX 的工作方式,这是否可行?我知道,可以使用其中节的标题和名称引用包,但是在最初设置它之后有没有办法更改/重新设置它\section{mytitle}

该场景如下:

我已经创建了一个自定义环境,其中序言总是以

\subsubsection{My Content No. \arabic{mycounter}}

然后创建一个表格,其中显示了本部分的所有信息。我设置了一些带参数\newcommands的命令,它们的作用只是生成格式化的表格行。所有这些都运行正常。但还有一个命令

\name{TitleOfThisCase}

并非所有情况下都会使用此命令,但当使用它时,我想用\subsubsection提供的标题覆盖命令的上述参数。默认情况下,该部分只是编号,如果提供了名称,则应该重置标题。简而言之,我正在寻找一些可以添加到命令定义中的东西,\name可以执行类似

\renewcommand{\currentname}{#1 (Case No. \arabic{mycounter})}

我的代码看起来有点像下面这样,使用超表格式包裹:

\documentclass[]{article}

\usepackage{supertabular}

\newcommand{\variants}[1]{\multicolumn{2}{l}{\emph{Variants:}} \\
\multicolumn{2}{p{\textwidth}}{#1}\\
\hline}
% ...
\newcommand{\name}[1]{Name & #1 \\ \hline }

\newcounter{myCount}
\newenvironment{myEnv}[1][\arabic{myCount}]{
\stepcounter{myCount}
\bgroup
\subsubsection{Env \arabic{myCount}}
\begin{center}
\tablefirsthead{
\hline}
\tablehead{%
\multicolumn{2}{l}{\scriptsize\sl myEnv \arabic{myCount} (continued)}\\
\hline}
\tabletail{%
\hline
\multicolumn{2}{r}{\scriptsize\sl continued on next page \dots}\\
\hline}
\tablelasttail{\hline}
\label{awf:#1}
\begin{supertabular}{p{3.5cm}|p{11.5cm}}
}{
\end{supertabular}
\end{center}
\egroup  
}

\begin{document}

\section{first section}
    \subsection{a subsection}
        \begin{myEnv}[label1]
            \name{My title (this should also reset the title of this section), it is Environment1}
            \variants{some variants to describe}
        \end{myEnv}

        \subsubsection{Another Subsection}
        Another subsubsection, not using the custom environment.

        \begin{myEnv}[label2]
            \variants{this only has variants, and is Environment2, but section 1.1.3}
        \end{myEnv}

\section{Second Section}
    \subsection{another subsection}
        \subsubsection{yet another subsubsection}
        nothing here
        \begin{myEnv}[label3]
            \name{This is my environment with number 3, but section number 2.1.2}
        \end{myEnv}

\end{document}

答案1

我认为,最好的方法是使用一种方法,无论是否给出,都可以key-value控制子小节标题的生成。但是,在这种情况下,可选参数无法保留,但也必须是键,即。name={}labellabel=myLabel

如果name={}指定了,它会生成相应的表格行并将其用于子部分标题,否则,表格行不存在并且标题为Env \arabic{myCount}

先来一张快照:

在此处输入图片描述

代码如下:

\documentclass[]{article}
\usepackage{hyperref}
\usepackage{etoolbox}
\usepackage{xkeyval}%
\usepackage{supertabular}
\newcounter{myCount}



\makeatletter
\define@key{CasesFamilyKey}{name}[]{%
\def\CasesKVMacroName{#1}%
}%

\define@key{CasesFamilyKey}{label}[]{%
\def\CasesKVMacroLabel{#1}%
}%
\makeatother



\newcommand{\CreateNameLine}{%
\ifdef{\CasesKVMacroName}{%
Name & \CasesKVMacroName \tabularnewline
\hline
}{%
}%
}%

\newcommand{\CreateSubSubSectionTitle}[1][]{%
\setkeys{CasesFamilyKey}{#1}%
\ifdef{\CasesKVMacroName}{%
\subsubsection{\CasesKVMacroName}}
{%
\subsubsection{Env \arabic{myCount}}
}%
}%

\newcommand{\variants}[1]{\multicolumn{2}{l}{\emph{Variants:}} \\
\multicolumn{2}{p{\textwidth}}{#1}\\
\hline}
% ...
\newcommand{\name}[1]{Name & #1\\ \hline}

\newenvironment{myEnv}[1][]{%
\setkeys{CasesFamilyKey}{#1}%
\stepcounter{myCount}
\bgroup
\CreateSubSubSectionTitle[#1]%
%\subsubsection{\CreateSectionTitle{}} %%% 
\begin{center}
\tablefirsthead{
\hline}
\tablehead{%
\multicolumn{2}{l}{\scriptsize\sl myEnv \arabic{myCount} (continued)}\\
\hline}
\tabletail{%
\hline
\multicolumn{2}{r}{\scriptsize\sl continued on next page \dots}\\
\hline}
\tablelasttail{\hline}
\label{awf:\CasesKVMacroLabel}%
\begin{supertabular}{p{3.5cm}|p{11.5cm}}
\CreateNameLine
}{%
\end{supertabular}
\end{center}
\egroup  
}


\begin{document}



\begin{myEnv}[name={My title},label=label1]
    \variants{some variants to describe}
\end{myEnv}

\begin{myEnv}[label=label2]
    \variants{this only has variants of \ref{awf:label1}}
\end{myEnv}

\begin{myEnv}[name={But this has a title again}, label=label3]
    \variants{These are also variants of \ref{awf:label1}}
\end{myEnv}


\end{document}

请注意,你必须说name={This is a long title with whitespace},但只需写一个单词标题即可name=foo

更新:正确计算环境数量的版本

\documentclass[]{article}
\usepackage{hyperref}
\usepackage{etoolbox}
\usepackage{xkeyval}%

\usepackage{supertabular}

\newcounter{myCount}[section]
\makeatletter
\define@key{CasesFamilyKey}{name}[]{%
\def\CasesKVMacroName{#1}%
}%

\define@key{CasesFamilyKey}{label}[]{%
\def\CasesKVMacroLabel{#1}%
}%
\makeatother



\newcommand{\CreateNameLine}{%
\ifdef{\CasesKVMacroName}{%
Name & \CasesKVMacroName \tabularnewline
\hline
}{%
}%
}%

\newcommand{\CreateSubSubSectionTitle}[1][]{%
\setkeys{CasesFamilyKey}{#1}%
\ifdef{\CasesKVMacroName}{%
\addtocounter{myCount}{-1}% 
\subsubsection{\CasesKVMacroName}}
{%
\subsubsection{Env \arabic{myCount}}
}%
}%

\newcommand{\variants}[1]{\multicolumn{2}{l}{\emph{Variants:}} \\
\multicolumn{2}{p{\textwidth}}{#1}\\
\hline}
% ...
\newcommand{\name}[1]{Name & #1\\ \hline}

\newenvironment{myEnv}[1][]{%
\setkeys{CasesFamilyKey}{#1}%
\refstepcounter{myCount}
\bgroup
\CreateSubSubSectionTitle[#1]%
%\subsubsection{\CreateSectionTitle{}} %%% 
\begin{center}
\tablefirsthead{
\hline}
\tablehead{%
\multicolumn{2}{l}{\scriptsize\sl myEnv \arabic{myCount} (continued)}\\
\hline}
\tabletail{%
\hline
\multicolumn{2}{r}{\scriptsize\sl continued on next page \dots}\\
\hline}
\tablelasttail{\hline}
\label{awf:\CasesKVMacroLabel}%
\begin{supertabular}{p{3.5cm}|p{11.5cm}}
\CreateNameLine
}{%
\end{supertabular}
\end{center}
\egroup  
}


\begin{document}
\tableofcontents%

\section{First}

\subsection{First sub}


\begin{myEnv}[name={My title},label=label1]
    \variants{some variants to describe}
\end{myEnv}

\begin{myEnv}[label=label2]
    \variants{this only has variants of \ref{awf:label1}}
\end{myEnv}

\subsubsection{A completely different subsubsection}

\begin{myEnv}[name={But this has a title again}, label=label3]
    \variants{These are also variants of \ref{awf:label1}}
\end{myEnv}

\section{Another one}

\subsection{Also another one}

\subsubsection{Yet another completely different subsubsection}


\begin{myEnv}[label=label5]
    \variants{These are also variants of \ref{awf:label4}}
\end{myEnv}

\begin{myEnv}[name={But this has a title again, but was shown already}, label=label4]
    \variants{These are also variants of \ref{awf:label3}}
\end{myEnv}


\begin{myEnv}[label=label6]
    \variants{These are also variants of \ref{awf:label4}}
\end{myEnv}

\begin{myEnv}[label=label7]
    \variants{These are also variants of \ref{awf:label4}}
\end{myEnv}


\end{document}

在此处输入图片描述

相关内容