4

General

I know there are question about "how to refer to a specific title" however I need a way to generally refer to the superior section's title or to the title of the current section of a specific level (similar to referring to a section's counter using \thesection).

My documentclass is article.

Problem

I have a tree like document where the title of each section describes an element in the tree. The section titles should be the path to the corresponding element.

enter image description here

However I don't want to specify the full path to the element in the section command (\subsubsection{A/B1/C}) as this can cause a lot of refactoring work when an element in the path changes.
I would prefer to only specify the leaf element (the last or tail element) of the path while the head is automatically deduced from the superior section. Something like:

% How should \currentsectionname and \currentsubsectionname be defined?
\newcommand{\treesubsection}[1]{\subsection{\currentsectionname /#1}}
\newcommand{\treesubsubsection}[1]{\subsubsection{\currentsubsectionname /#1}}

\section{A}
\treesubsection{B1}
\treesubsubsection{C}

I tried the solutions from this question however they only seem to work when used within a section's body but not within the title:

  • The nameref solution gives me TeX capacity exceeded, sorry [input stack size=5000].
  • The titleref solution duplicates the leaf title (i.e. results in for example A/B1/B1 instead of A/B1).
  • The zref-titleref solution puts a "currentname" instead of the head and also duplicates the leaf.

Question

How can I refer to the superior section's title or to the title of the current section of a specific level, similar to \thesection, \thesubsection, ... ?
In addition I want to use the feature that I can choose a custom section title for the TOC, as it is possible for \section[custom toc title]{title}. Actually the custom title should always be the leaf name.

EDIT (what I tried specifically)

nameref

\usepackage{nameref}
\makeatletter
\newcommand*{\currentname}{\@currentlabelname}
\makeatother

\newcommand{\treesubsection}[1]{\subsection{\currentname /#1}}
\newcommand{\treesubsubsection}[1]{\subsubsection{\currentname /#1}}

titleref

\usepackage{titleref}
\makeatletter
\newcommand*{\currentname}{\TR@currentTitle}
\makeatother

\newcommand{\treesubsection}[1]{\subsection{\currentname /#1}}
\newcommand{\treesubsubsection}[1]{\subsubsection{\currentname /#1}}

zref-titleref

\usepackage{zref-titleref}
\makeatletter
\newcommand*{\currentname}{\zref@getcurrent{title}}
% \newcommand*{\currentname}{\zref@titleref@current}
\makeatother

\newcommand{\treesubsection}[1]{\subsection{\currentname /#1}}
\newcommand{\treesubsubsection}[1]{\subsubsection{\currentname /#1}}
  • Please provide at least a version of your document that has this structure and show what you have tried with nameref etc. – user31729 Jul 14 '16 at 10:58
3

Using \newcommand{\currentname}{\@currentlabelname} has no effect. It would refer to the current value of \@currentlabelname or is even undefined. In fact, the \@currentlabelname must be stored in an expansion step to another macro after the section title has been set, not in the argument.

Please note that the current definition of \tree... misses the possibility of using \treesection[short toc title]{long title}.

enter image description here

\documentclass{article}

\usepackage{nameref}
\makeatletter

\newcommand{\treesection}[1]{\section{#1}
 \edef\currentsectionname{\@currentlabelname}}

\newcommand{\treesubsection}[1]{\subsection{\currentsectionname/#1}\edef\currentsubsectionname{\@currentlabelname}}
\newcommand{\treesubsubsection}[1]{\subsubsection{\currentsubsectionname /#1}\edef\currentsubsubsectionname{\@currentlabelname}}
\makeatother


\begin{document}

\treesection{A}
\treesubsection{B1}
\treesubsection{B2}
\treesubsubsection{C1}


\treesection{Z}
\treesubsection{B1}
\treesubsection{B2}
\treesubsubsection{C1}


\end{document}

Update 'Automatic' definition of a tree level and declaration of the \current...name with xparse:

\documentclass{book}

\usepackage{nameref}

\usepackage{xparse}

\setcounter{secnumdepth}{3}

\makeatletter

\NewDocumentCommand{\DefineTreeLevel}{om}{%
  \expandafter\NewDocumentCommand\csname tree#2\endcsname{som}{%
    \IfBooleanTF{##1}{%
      \csname #2*\endcsname{##3}
    }{%
      \def\@@tempa@@{}%
      \IfValueT{#1}{%
        \def\@@tempa@@{\csname current#1name\endcsname/}
      }
      \IfValueTF{##2}{%
        \csname #2\endcsname[##2]{\@@tempa@@##3}%
      }{%
        \csname #2\endcsname{\@@tempa@@##3}%
      }%
      \expandafter\edef\csname current#2name\endcsname{\@currentlabelname}%
    }%
  }%
}

\makeatother


\DefineTreeLevel{chapter}
\DefineTreeLevel[chapter]{section}
\DefineTreeLevel[section]{subsection}
\DefineTreeLevel[subsection]{subsubsection}


\begin{document}

\treechapter{Gamma}
\treesection{A1}
\treesubsection{B1}
\treesubsection{B2}
\treesubsubsection{C1}


\treesection{Z}
\treesubsection{B1}
\treesubsection{B2}
\treesubsubsection{C1}


\end{document}

Update

ToC - related stuff with some screenshot

\documentclass{article}

\usepackage{nameref}

\usepackage{xparse}

\setcounter{secnumdepth}{3}

\makeatletter

\NewDocumentCommand{\DefineTreeLevel}{om}{%
  \expandafter\NewDocumentCommand\csname tree#2\endcsname{som}{%
    \IfBooleanTF{##1}{%
      \csname #2*\endcsname{##3}
    }{%
      \def\@@tempa@@{}%
      \IfValueT{#1}{%
        \def\@@tempa@@{\csname current#1name\endcsname/}
      }
      \IfValueTF{##2}{%
        \csname #2\endcsname[##2]{\@@tempa@@##3}%
      }{%
        \csname #2\endcsname{\@@tempa@@##3}%
      }%
      \expandafter\edef\csname current#2name\endcsname{\@currentlabelname}%
    }%
  }%
}

\makeatother


\DefineTreeLevel{section}
\DefineTreeLevel[section]{subsection}
\DefineTreeLevel[subsection]{subsubsection}


\begin{document}
\tableofcontents
\treesection{A1}
\treesubsection[B1]{B1}
\treesubsection{B2}
\treesubsubsection[C1]{C1}


\treesection{Z}
\treesubsection{B1}
\treesubsection{B2}
\treesubsubsection{C1}


\end{document}

enter image description here

Next update, recursive \@current.... names:

\documentclass{article}

\usepackage{nameref}

\usepackage{xparse}

\setcounter{secnumdepth}{3}

\makeatletter

\NewDocumentCommand{\DefineTreeLevel}{om}{%
  \expandafter\NewDocumentCommand\csname tree#2\endcsname{som}{%
    \IfBooleanTF{##1}{%
      \csname #2*\endcsname{##3}%
    }{%
      \gdef\@@tempa@@{}%
      \IfValueT{#1}{%
        \def\@@tempa@@{\csname current#1name\endcsname/}
      }%
      \IfValueTF{##2}{%
        \csname #2\endcsname[##2]{\@@tempa@@##3}%
        \expandafter\edef\csname current#2name\endcsname{\@@tempa@@\@currentlabelname}%
      }{%
        \csname #2\endcsname{\@@tempa@@##3}%
        \expandafter\edef\csname current#2name\endcsname{\@currentlabelname}%
      }%
    }%
  }%
}

\makeatother


\DefineTreeLevel{section}
\DefineTreeLevel[section]{subsection}
\DefineTreeLevel[subsection]{subsubsection}


\begin{document}
\tableofcontents
\treesection{A1}
\treesubsection[B1]{B1}
\treesubsubsection[C1]{C1}
\treesubsection[B2]{B2}
\treesubsubsection[C2]{C2}
\treesubsubsection[C3]{C3}


\treesection{Z}
\treesubsection{B1}
\treesubsection{B2}
\treesubsubsection{C1}


\end{document}
  • Nice solution! Unfortunately it seems not to work with customizing the TOC title of the section. When I do \treesubsubsection[B2]{B2} and \treesubsubsection[C1]{C1} it shows B2 and C1 in the TOC however it also cuts the A1 from A1/B2/C1's section title in the body. Because section names can become quite large like this I would like to have only the leaf name in the TOC. – a_guest Jul 14 '16 at 19:00
  • @a_guest: You would have the same trouble with your 'original' \tree... macros, woudn't you? – user31729 Jul 14 '16 at 19:05
  • I don't see the point. Is the current section name deduced from what's in the TOC? And if so, can't we just store the current section name in another variable and retrieve it from there? Keeping the TOC and actual section names (those in the body) separated? – a_guest Jul 14 '16 at 19:12
  • @a_guest: Are we talking about the first or the second version? – user31729 Jul 14 '16 at 19:16
  • I am referring to your second version which uses xparse. – a_guest Jul 14 '16 at 19:17

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.