Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
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
You can use the option settings: \mdfsetup{settings={\gdef\mdframetitlemark{\mdf@frametitle}}} –  Marco Daniel Jul 8 '12 at 10:52

4 Answers 4

up vote 12 down vote accepted

With this solution you can still use the stared versions or optional arguments of \chapter et al


\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''.
share|improve this answer
very clever! +1 –  tohecz Jul 5 '12 at 9:08
No, it won't work with the starred versions. –  masu Nov 8 '13 at 16:12
@masu: it does work! –  Herbert Apr 20 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 at 16:47
remember that a starred version of \section et al are not part of the TOC and not listed in the header! –  Herbert May 4 at 17:57

Change the \@sect macro in latex.ltx for

\xdef\sectionname{#8}% we change only here. 
  \ifnum #2>\c@secnumdepth
  \@tempskipa #5\relax
    \ifdim \@tempskipa>\z@
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \csname #1mark\endcsname{#7}%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      #6{\hskip #3\relax
      \@svsec #8}%
      \csname #1mark\endcsname{#7}%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%

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

share|improve this answer
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 at 18:50

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.






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


share|improve this answer
This is the first thing that came to one's mind, but a problem arises when you want to use \chapter* –  tohecz Jul 5 '12 at 9:08

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

\usepackage{nameref}% http://ctan.org/pkg/nameref
\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}.

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.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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