1

I'm trying to write a lab manual. Because of this I need a consistent and generalized method to label a new page heading (not header) with the title of the section (the activity name).

\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}

I have tried the solutions Here using

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

but this returns the current subsection title "Theory" when I need it to return the section title "Lab1 Title". Is there any way to call the current lable 1 level up?

  • Welcome! Please fix your code so it does not give an error when we try to compile it. I don't really understand the problem. Isn't \chapter or \section doing what you want? How did you try to use \currentname? I'm trying to figure out where the new thing should be and what its content should be. – cfr Feb 23 '18 at 3:24
  • 1
    Thanks. What about using \label after the \section and then using the nameref package to insert its name again to make the repeat in the table? – cfr Feb 23 '18 at 3:38
0
\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}

Here is a way without using "label" at all (As you asked).

Check the output to see if it is ok for you.

(Always keeping in \mysec variable the last section's title)

Edit: (to allow linebreaks in titles of sections and in reference too)

\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
  • That worked. It somehow also disabled the ability to use a carriage return \\ in a section title, but I can live with that. Thanks! – Jeff Feb 23 '18 at 3:44
  • @Jeff I used \def for the definitions.. If you want to have linebreaks (\\) in section titles I can fix it to use \long\def and it will work... But then do you want this line break to appear in the reference too? or you prefer to remove it from the reference? – koleygr Feb 23 '18 at 3:52
  • It works how I need it a the moment. It's just dumb luck I had something with a linebreak in it at the moment. The state this manual is in is still the super early stages and it has yet to be seen if linebreaks in titles will be an issue. – Jeff Feb 23 '18 at 3:59
  • @Jeff ok... Added a way to let you use \\\ too (in section and it's reference) in case you (or anyone) need it. – koleygr Feb 23 '18 at 4:09
1

I would create a wrapper for \section, I think, which saves the name as well as starting a new section. You could also extend your existing macro for the second part by allowing it to take an optional argument. I've set things so that, in this case, it will typeset the optional argument followed by a page break. otherwise, No page break is inserted. Since you always want a break before the tear-away part, however, I've added that in unconditionally.

I've found that this kind of usage usually benefits from quite specific, tailored macros and environments because, basically, I want to repeat the same format consistently with just slightly different content: a different assignment name, a different student ID, a different module code .... So my teaching macros tend to be highly task-specific because I need those things repeatedly. Options then allow for slight variations and standard macros, such as \section are still available if I want to interleave less structured material.

I would actually probably go much further than this :-). However, your kilometres may, as always, vary.

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}

5 pages of lab activities

  • Interesting. Will that play well with a table of contents? I wanted to define different types of sections for different types of activities; lab activities, pre-lab activities, and mini-lab activities. I created environments specific to each that change the section labels using the titlesec package and \titleformat, but it requires the environment to encompass the entire chapter of the lab manual for each of the 3 types of activities. Creating unique wrappers, as long as \tableofcontents still sees them as sections, might be more elegant. – Jeff Feb 23 '18 at 20:42
  • @Jeff It is just a wrapper. If you use a *, it won't end up in the ToC, as usual. If you don't use one, it will end up in the ToC. It will also set marks for headers and footers and so on, if you need those. All it is doing is \section and a little bit more. But the \section stuff is not touched at all. – cfr Feb 23 '18 at 23:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.