ラベルを使用せずにサブセクション内から現在のセクション タイトルを呼び出すにはどうすればよいですか?

ラベルを使用せずにサブセクション内から現在のセクション タイトルを呼び出すにはどうすればよいですか?

ラボマニュアルを作成しようとしています。このため、新しいページの見出し (ヘッダーではない) にセクションのタイトル (アクティビティ名) のラベルを付ける、一貫性のある一般的な方法が必要です。

\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思います。これにより、名前が保存され、新しいセクションが開始されます。また、2 番目の部分で既存のマクロを拡張して、オプションの引数を取ることもできます。この場合、オプションの引数の後にページ区切りがタイプセットされるように設定しました。それ以外の場合は、ページ区切りは挿入されません。ただし、切り離し部分の前には常に区切りが必要なので、無条件にそれを追加しました。

こうした使用法では、通常、かなり特殊でカスタマイズされたマクロと環境が役立つことがわかりました。基本的に、同じ形式を一貫して繰り返し、わずかに異なるコンテンツ (異なる課題名、異なる学生 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ページ

関連情報