# how to get the current chapter name, section name, subsection name, etc?

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

With this solution you can still use the stared versions or 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 –  tohecz Jul 5 '12 at 9:08
–  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

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

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

-

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}%
\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}%
\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 at 18:50
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 at 6:45

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

\newcommand{\currentsection}{}
\let\oldsection\section

\newcommand{\currentsubsection}{}
\let\oldsubsection\subsection

\newcommand{\currentsubsubsection}{}
\let\oldsubsubsection\subsubsection

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

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

\end{document}

-
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:

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

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