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

a_guest
  • 351
  • Please provide at least a version of your document that has this structure and show what you have tried with nameref etc. –  Jul 14 '16 at 10:58

1 Answers1

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? –  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? –  Jul 14 '16 at 19:16
  • I am referring to your second version which uses xparse. – a_guest Jul 14 '16 at 19:17
  • @a_guest: \treesubsubsection[B2]{B2} should give B2 in the ToC and A1/B2 in the body, right? –  Jul 14 '16 at 19:21
  • If you mean \treesubsection[B2]{B2} (only 1 sub), then yes. However \treesubsubsection[C1]{C1} gives C1 in the TOC but B2/C1 in the body. – a_guest Jul 14 '16 at 19:25
  • @a_guest: Are you using the version with \treechapter? –  Jul 14 '16 at 19:26
  • Ah forgot to mention, I am using \documentclass{article} and removed \DefineTreeLevel{chapter} and used \DefineTreeLevel{section} instead. Is that a problem? – a_guest Jul 14 '16 at 19:27
  • @a_guest: See the updated code with article only and the screen shot -- \treesubsubsection[C1][C1] gives C1 in the ToC and A1/B2/C1 in the body. And again, your original \tree.... macros would not even distinguish between ToC and body ... so this ToC issue is actually a question change, in my point of view –  Jul 14 '16 at 19:29
  • Because you didn't use \treesubsection[B2]{B2}. If I want only B2 (instead of A1/B2) in the TOC, then it won't work for C1 in the body. – a_guest Jul 14 '16 at 19:32
  • I don't think it's another question, it's just that my question is more specific. I not only want to refer to superior sections and thus obtaining a tree like section structure, I also want to be able to choose any TOC title for any section (as it is possible with "normal" sections). Using section, subsection, ... I can choose an arbitrary TOC title without invalidating the body's text. I would like to do the same using the tree structure. – a_guest Jul 14 '16 at 19:49
  • @a_guest: See the next update at the bottom of my answer –  Jul 14 '16 at 20:36
  • Yes that now really solves my problem! Thank you a lot! I don't fully understand your solution, so if it's not a big effort could you maybe add some comments to your macro that explain what's happening? – a_guest Jul 14 '16 at 20:56
  • @a_guest: I'll try later on (perhaps in a few days, I won't have access to LaTeX for the next few days), is that o.k? –  Jul 14 '16 at 20:59