如何在不使用标签的情况下从子部分中调用当前部分标题?

如何在不使用标签的情况下从子部分中调用当前部分标题?

我正在尝试编写实验室手册。因此,我需要一种一致且通用的方法来用章节标题(活动名称)标记新页面标题(不是页眉)。

\documentclass[12pt]{report}
\usepackage{nameref}
\usepackage{tabularx}
\newcolumntype{J}{>{\centering\arraybackslash}X}
\makeatletter
\newcommand*{\currentname}{\@currentlabelname}
\makeatother

\newcommand{\handinhead}{%
    \begin{center}
        \begin{table}[!h]
            \begin{tabularx}{\textwidth}{X J X}
                                 & \currentname &                  \\
                Name:~\hrulefill &              & Date:~\hrulefill \\
            \end{tabularx}
        \end{table}
    \end{center}
    \hrule
    }
\begin{Document}
\chapter{Lab Activities}    
\section{Lab1 Title}
        Here is the activity introduction.
        \subsection{Objective}
            Objective of the activity.
        \subsection{Equipment}
            A list of equipment needed.
        \subsection{Theory}
            Explanation of the theory.
\newpage
\handinhead
    The following is the hand in portion of the activity.

\end{document}

我已经尝试过解决方案这里使用

\makeatletter
\newcommand*{\currentname}{\@currentlabelname}
\makeatother

但是当我需要返回章节标题“Lab1 Title”时,它却返回了当前子章节标题“Theory”。有没有办法调用当前 1 级标签?

答案1

\documentclass[12pt]{report}

\let\oldsection\section
\makeatletter
\def\section{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred chapter can not have parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%
\xdef\mysec{#2}\oldsection{#2}
}
\def\@StarredWithout#1{%
\xdef\mysec{#1}\oldsection*{#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\xdef\mysec{#2}\oldsection[#1]{#2}%
}
\def\@nonStarredWithout#1{%
\xdef\mysec{#1}\oldsection{#1}%
}
\makeatother

\begin{document}
\chapter{Lab Activities}    
\section{Lab1 Title}
        Here is the activity introduction.
        \subsection{Objective}
            Objective of the activity.
        \subsection{Equipment}
            A list of equipment needed.
        \subsection{Theory}
            Explanation of the theory.
\newpage
\hrule
Here we are in \mysec.
\hrule
The following is the hand in portion of the activity.



\end{document}

这是一种完全不使用“标签”的方法(正如您所问的)。

检查输出看看是否适合您。

(始终保持\mysec最后一节的标题不变)

编辑:(允许在章节标题和参考文献中换行)

\let\oldsection\section
\makeatletter
\long\def\section{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred chapter can not have parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\long\def\@StarredWith[#1]#2{%
\long\def\mysec{#2}\oldsection{#2}
}
\long\def\@StarredWithout#1{%
\long\def\mysec{#1}\oldsection*{#1}%
}
\long\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\long\def\@nonStarredWith[#1]#2{%
\long\def\mysec{#2}\oldsection[#1]{#2}%
}
\long\def\@nonStarredWithout#1{%
\long\def\mysec{#1}\oldsection{#1}%
}
\makeatother

答案2

我想,我会为创建一个包装器\section,它保存了名称并开始新的部分。您还可以通过允许它接受可选参数来扩展第二部分的现有宏。我已经设置了一些东西,在这种情况下,它将排版可选参数,然后是分页符。否则,不会插入分页符。但是,由于您总是希望在撕掉部分之前有一个分隔符,因此我无条件地添加了它。

我发现这种用法通常受益于非常具体、量身定制的宏和环境,因为基本上,我想重复相同的格式,内容略有不同:不同的作业名称、不同的学生 ID、不同的模块代码……所以我的教学宏往往高度针对任务,因为我需要重复这些东西。然后选项允许轻微的变化和标准宏,例如,\section如果我想穿插不太结构化的材料,这些仍然可用。

我实际上可能会走得比这更远 :-)。但是,您的里程数可能一如既往地有所不同。

句法:

  • \labsection[<short title>]{<title>}
  • \labsection{<title>}
  • \labsection*{<title>}
  • \handinhead[<text>]
  • \handinhead

\documentclass[12pt]{report}
\usepackage{tabularx,xparse}
\newcolumntype{J}{>{\centering\arraybackslash}X}
\newcommand*\labname{}
\NewDocumentCommand\labsection{som}{%
  \renewcommand*\labname{#3}%
  \IfBooleanTF{#1}{%
    \section*{#3}%
  }{%
    \IfValueTF{#2}{%
      \edef\tempa{#2}%
    }{%
      \edef\tempa{#3}%
    }%
    \section[\tempa]{#3}%
  }%
}
\NewDocumentCommand{\handinhead}{+o}{%
  \clearpage
  \begin{center}
    \begin{tabularx}{\textwidth}{X J X}
      & \labname &   \\
      Name:~\hrulefill & & Date:~\hrulefill \\
    \end{tabularx}
  \end{center}
  \hrule\medskip\par
  \IfValueT{#1}{#1\clearpage}%
}
\begin{document}
\chapter{Lab Activities}    
\labsection{Lab1 Title}
Here is the activity introduction.
\subsection{Objective}
Objective of the activity.
\subsection{Equipment}
A list of equipment needed.
\subsection{Theory}
Explanation of the theory.
\handinhead
[The following is the hand in portion of the activity.]

\labsection[Lab2]{Lab2 Title}
Here is the activity introduction.
\subsection{Objective}
Objective of the activity.
\subsection{Equipment}
A list of equipment needed.
\subsection{Theory}
Explanation of the theory.
\handinhead
The following is the hand in portion of the activity.

\labsection*[Lab3]{Lab3 Title}
Here is the activity introduction.
\subsection{Objective}
Objective of the activity.
\subsection{Equipment}
A list of equipment needed.
\subsection{Theory}
Explanation of the theory.
\handinhead
The following is the hand in portion of the activity.

\end{document}

5 页实验室活动

相关内容