
Я пытаюсь написать руководство по лабораторной работе. В связи с этим мне нужен последовательный и обобщенный метод для обозначения заголовка новой страницы (не заголовка) с названием раздела (названием деятельности).
\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
но это возвращает текущий заголовок подраздела "Theory", когда мне нужно вернуть заголовок раздела "Lab1 Title". Есть ли способ вызвать текущий 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
, я думаю, которая сохраняет имя, а также начинает новый раздел. Вы также можете расширить свой существующий макрос для второй части, разрешив ему принимать необязательный аргумент. Я настроил все так, что в этом случае он наберет необязательный аргумент, за которым последует разрыв страницы. в противном случае разрыв страницы не вставляется. Поскольку вы всегда хотите разрыв перед отрывной частью, я добавил его безоговорочно.
Я обнаружил, что такой тип использования обычно выигрывает от довольно специфических, адаптированных макросов и сред, потому что, по сути, я хочу повторять один и тот же формат последовательно с немного другим содержанием: другое название задания, другой идентификатор студента, другой код модуля... Поэтому мои учебные макросы, как правило, очень специфичны для задач, потому что мне эти вещи нужны неоднократно. Затем опции позволяют вносить небольшие изменения и использовать стандартные макросы, такие как те, которые \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}