如何在不使用標籤的情況下從小節中呼叫當前節標題?

如何在不使用標籤的情況下從小節中呼叫當前節標題?

我正在嘗試寫一本實驗室手冊。因此,我需要一種一致且通用的方法來用該部分的標題(活動名稱)來標記新頁面標題(不是頁首)。

\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”。有什麼辦法可以把目前的lable 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頁實驗室活動

相關內容