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

5 Answers 5

up vote 15 down vote accepted

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}
share|improve this answer
    
very clever! +1 –  yo' 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

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.

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

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

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
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 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
\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}
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* –  yo' Jul 5 '12 at 9:08

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

Your Answer

 
discard

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.