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.
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 meTeX capacity exceeded, sorry [input stack size=5000]
. - The
titleref
solution duplicates the leaf title (i.e. results in for exampleA/B1/B1
instead ofA/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}}
nameref
etc.