
Ich versuche, ein Laborhandbuch zu schreiben. Aus diesem Grund benötige ich eine konsistente und allgemeine Methode, um eine neue Seitenüberschrift (keine Kopfzeile) mit dem Titel des Abschnitts (dem Aktivitätsnamen) zu kennzeichnen.
\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}
Ich habe die Lösungen ausprobiertHiermit
\makeatletter
\newcommand*{\currentname}{\@currentlabelname}
\makeatother
aber dies gibt den aktuellen Unterabschnittstitel „Theorie“ zurück, obwohl ich den Abschnittstitel „Lab1-Titel“ zurückgeben muss. Gibt es eine Möglichkeit, das aktuelle Label eine Ebene höher aufzurufen?
Antwort1
\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}
Hier ist eine Möglichkeit, überhaupt ohne „Label“ auszukommen (wie Sie gefragt haben).
Überprüfen Sie die Ausgabe, um zu sehen, ob sie für Sie in Ordnung ist.
( \mysec
Der Titel des letzten Abschnitts muss immer variabel bleiben)
Bearbeiten: (um Zeilenumbrüche auch in Abschnittstiteln und Referenzen zuzulassen)
\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
Antwort2
Ich würde, glaube ich, einen Wrapper für erstellen \section
, der den Namen speichert und einen neuen Abschnitt beginnt. Sie könnten Ihr vorhandenes Makro für den zweiten Teil auch erweitern, indem Sie ihm erlauben, ein optionales Argument anzunehmen. Ich habe es so eingestellt, dass in diesem Fall das optionale Argument gefolgt von einem Seitenumbruch gesetzt wird. Andernfalls wird kein Seitenumbruch eingefügt. Da Sie jedoch immer einen Umbruch vor dem Abreißteil wünschen, habe ich diesen unbedingt hinzugefügt.
Ich habe festgestellt, dass diese Art der Nutzung normalerweise von ziemlich spezifischen, maßgeschneiderten Makros und Umgebungen profitiert, da ich im Grunde dasselbe Format konsistent mit nur leicht unterschiedlichem Inhalt wiederholen möchte: ein anderer Aufgabenname, eine andere Studenten-ID, ein anderer Modulcode … Meine Lehrmakros sind daher in der Regel sehr aufgabenspezifisch, da ich diese Dinge wiederholt benötige. Optionen ermöglichen dann leichte Variationen und Standardmakros, wie sie \section
weiterhin verfügbar sind, wenn ich weniger strukturiertes Material einflechten möchte.
Ich würde wahrscheinlich sogar noch viel weiter fahren :-). Die Kilometerzahl kann jedoch, wie immer, abweichen.
Syntax:
\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}