50

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

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

5 Answers 5

35

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}
7
  • very clever! +1
    – yo'
    Jul 5, 2012 at 9:08
  • 1
    No, it won't work with the starred versions.
    – masu
    Nov 8, 2013 at 16:12
  • @masu: it does work!
    – user2478
    Apr 20, 2014 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, 2014 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, 2014 at 17:57
32

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.

4
  • 12
    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. Jul 5, 2012 at 6:58
  • e.g., \newcommand{\currentchapter}{} \newcommand{\mychapter}[1]{\chapter{#1}\renewcommand{\currentchapter}{#1}} and use \mychapter in place of \chapter Jul 5, 2012 at 7:00
  • 1
    then \currentchapter contains the argument passed to the current \mychapter. I was hoping to avoid redefining or wrapping all the possible sectioning macros. Jul 5, 2012 at 7:01
  • This approach still works, in 2021!
    – Michelle
    Jan 27, 2021 at 19:43
7

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}
4
  • 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, 2012 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, 2018 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, 2018 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, 2019 at 22:40
3

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".

2
  • 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, 2014 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, 2014 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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .