37

How can one get the current name of the following: Chapter, Section, Subsection(s), frame, label, mdframed?

  • What do you mean with mdframed? – Marco Daniel Jul 5 '12 at 20:10
  • @MarcoDaniel mdframed's frametitle – AbstractDissonance Jul 5 '12 at 21:35
  • 1
    You can use the option settings: \mdfsetup{settings={\gdef\mdframetitlemark{\mdf@frametitle}}} – Marco Daniel Jul 8 '12 at 10:52
28

With this solution you can still use the starred versions and optional arguments of \chapter et al.

\documentclass{book}
\let\Chaptermark\chaptermark
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}
\let\Sectionmark\sectionmark
\def\sectionmark#1{\def\Sectionname{#1}\Sectionmark{#1}}
\let\Subsectionmark\subsectionmark
\def\subsectionmark#1{\def\Subsectionname{#1}\Subsectionmark{#1}}
\let\Subsubsectionmark\subsubsectionmark
\def\subsubsectionmark#1{\def\Subsubsectionname{#1}\Subsubsectionmark{#1}}

\begin{document}
\chapter{First chapter}\label{ch:first}
This is chapter~\ref{ch:first} with title ``\Chaptername''.
\section{First section}\label{sec:first}
This is section~\ref{sec:first} with title ``\Sectionname''.
\subsection{The first subsection}\label{subsec:first}
This is subsection~\ref{subsec:first} with title ``\Subsectionname''.
\subsubsection{Last subsubsection}\label{subsubsec:first}
This is subsubsection~\ref{subsubsec:first} with title ``\Subsubsectionname''.
\end{document}
  • very clever! +1 – yo' Jul 5 '12 at 9:08
  • 1
    No, it won't work with the starred versions. – masu Nov 8 '13 at 16:12
  • @masu: it does work! – user2478 Apr 20 '14 at 5:51
  • @Herbert: I beg to differ... but maybe that was because of the magical ways of MikTeX, or we were mistaken somewhere. – masu May 4 '14 at 16:47
  • remember that a starred version of \section et al are not part of the TOC and not listed in the header! – user2478 May 4 '14 at 17:57
25

In terms of section titles, the nameref package is ideal for this. It provides \nameref{<label>} that returns the sectional title associated with the label:

enter image description here

\documentclass{article}
\usepackage{nameref}% http://ctan.org/pkg/nameref
\begin{document}
\section{First section}\label{first}
This is section~\ref{first} with title \nameref{first}.
\subsection{Second subsection}\label{second}
This is subsection~\ref{second} with title \nameref{second}.
\subsubsection{Last subsubsection}\label{third}
This is subsubsection~\ref{third} with title \nameref{third}.
\end{document}

hyperref provides similar functionality, since it loads nameref. As such, they work together without problem, allowing hyperlinked titles. memoir does something similar to store the names/titles of sectional units.

This could be expanded to include the capture of names/titles associated with other environments or structures as well.

  • 7
    But I need the current section. With your labels I need to know the name already(essentially). i.e., you are just equating "first" with "first section", etc. Also, one has to specify the label each time. I could redefine section, subsection, chapter, etc but it seems like a lot of work. – AbstractDissonance Jul 5 '12 at 6:58
  • e.g., \newcommand{\currentchapter}{} \newcommand{\mychapter}[1]{\chapter{#1}\renewcommand{\currentchapter}{#1}} and use \mychapter in place of \chapter – AbstractDissonance Jul 5 '12 at 7:00
  • then \currentchapter contains the argument passed to the current \mychapter. I was hoping to avoid redefining or wrapping all the possible sectioning macros. – AbstractDissonance Jul 5 '12 at 7:01
5

Here is one solution but is not optimal. It simply renew's the old sectioning commands and have the new ones update the currentxxx macros.

\begin{document}

\newcommand{\currentchapter}{}
\let\oldchapter\chapter
\renewcommand{\chapter}[1]{\oldchapter{#1}\renewcommand{\currentchapter}{#1}}

\newcommand{\currentsection}{}
\let\oldsection\section
\renewcommand{\section}[1]{\oldsection{#1}\renewcommand{\currentsection}{#1}}

\newcommand{\currentsubsection}{}
\let\oldsubsection\subsection
\renewcommand{\subsection}[1]{\oldsubsection{#1}\renewcommand{\currentsubsection}{#1}}

\newcommand{\currentsubsubsection}{}
\let\oldsubsubsection\subsubsection
\renewcommand{\subsubsection}[1]{\oldsubsubsection{#1}\renewcommand{\currentsubsubsection}{#1}}


\chapter{chapter test}
\section{section test}
\subsection{subsection test}

\currentchapter\\
\currentsection\\
\currentsubsection\\

\end{document}
  • 2
    This is the first thing that came to one's mind, but a problem arises when you want to use \chapter* – yo' Jul 5 '12 at 9:08
  • 1
    My dirty extension for the stared versions (requires xparse package) : \newcommand{\currentchapter}{}\let\oldchapter\chapter\RenewDocumentCommand{\chapter}{sm}{ \IfBooleanTF{#1}{\oldchapter*{#2}}{\oldchapter{#2} }\renewcommand{\currentchapter}{#1}} – bonanza Jul 22 '18 at 17:07
  • There is a small typo in the last part of my previous comment, it should be \renewcommand{\currentchapter}{#2} – bonanza Jul 22 '18 at 20:58
  • The "dirty extension" gives me something useful, but for starred chapters like Index and References I get the name of the last chapter in the header. Would it be possible to include something to set the header to empty in a starred chapter? I played around a bit but couldn't find anything that works myself. – GaRyu Apr 6 at 22:40
2

Change the \@sect macro in latex.ltx for

%%%%
%%%%%%%
\def\@sect#1#2#3#4#5#6[#7]#8{%
\xdef\sectionname{#8}% we change only here. 
%\xdef\leftmark{#8}
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
     \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
    \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
          \endgroup
    \csname #1mark\endcsname{#7}%
     \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
        \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
  \fi
  \@xsect{#5}}
%%%%%%%%%%%
%%%%%%%%%%%

Now, you can use \sectionname after calling \section{....}. For example, if \section{Test} is used, we get \sectionname expands to "Test".

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. In particular, when inserting code, highlight the text to be formatted and then press the {} button. – Pier Paolo Aug 29 '14 at 18:50
  • 1
    This will, of course, not capture the chapter titles, nor be able to work in a true reference-like fashion where (title reference before section). – Werner Sep 24 '14 at 6:45
2

titleref provides similar functionality to nameref, but is not compatible with hyperref:

enter image description here

\documentclass{article}
\usepackage{titleref}
\begin{document}
\section{First section}\label{first}
This is section~\ref{first} with title \titleref{first}.
\subsection{Second subsection}\label{second}
This is subsection~\ref{second} with title \titleref{second}.
\subsubsection{Last subsubsection}\label{third}
This is subsubsection~\ref{third} with title \titleref{third}.
\end{document}

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.