3

I'm writing a URL that contains &sections= part. I would like to add a macro after &sections=, so that whenever the URL is opened, the current section title will be filled in dynamically.

I tried \thepage and \thesection which work both well and display the current page number or section number. However, what I want is the section titles, for both numbered and unnumbered sections.

I've tried two options provided in this post.

First:

\documentclass{article}

\newcommand*\nowtitle{}

\let\oldsectionmark=\sectionmark \renewcommand\sectionmark[1]{% \renewcommand\nowtitle{#1}% \oldsectionmark{#1}}

\begin{document}

\section{The first section}

\nowtitle

\section*{The second section}

\nowtitle -- (It shows The first section", but should beThe second section")

\end{document}

The problem is unnumbered sections show the previous numbered section title.

Second:

\documentclass{article}

\usepackage{etoolbox}

\makeatletter \patchcmd{@sect}% <cmd> {\ifnum}% <search> {\edef\nowtitle{#7}\ifnum}% <replace> {}{}% <success><failure> \patchcmd{@ssect}% <cmd> {@tempskipa}% <search> {\edef\nowtitle{#5}@tempskipa}% <replace> {}{}% <success><failure> \makeatother

\begin{document}

\section{The first section}

\nowtitle

\section*{The second section}

\nowtitle

\end{document}

It works well as minimal working example, but does not work in our project. It seems it has conflicts with the existing packages:

\usepackage[bookmarksopen=true,bookmarksnumbered=true]{hyperref}
\usepackage{nameref}
\usepackage{titlesec}
Carrie
  • 31
  • 2
  • Hi! I tried @Henri Menke approach and changed "chapter" to "section". It works close to what I want! However, it works only for numbered sections. I have both numbered and unnumbered sections. Do you have better solution? Thanks very much! – Carrie Aug 11 '23 at 07:15
  • 1
    You should always provide a small complete example so that people can test your issue and solutions. \leftmark should only be used in the output routine, that means only in the header or footer, in the text use nameref and @currentlabelname. – Ulrike Fischer Aug 11 '23 at 07:26
  • Hi Ulrike, thanks for the hint! I tried \nameref and @currentlabelname, but I was not able to extract only the section titles (not subsections or subsubsections). Do you have any idea? Thanks! – Carrie Aug 11 '23 at 08:20
  • 1
    what should \sections be in your example?? – Ulrike Fischer Aug 11 '23 at 08:38
  • I changed \sections to §ions to be consistent with the description text. The gist is what is wanted after §ions= is the current section title. – Carrie Aug 15 '23 at 08:44
  • 1

1 Answers1

1

All of the additional packages in your actual use case redefine or influence the sectional units. With titlesec in play, the title of a sectional unit is available through \ttl@savetitle.

enter image description here

\documentclass{article}

\usepackage[bookmarksopen=true,bookmarksnumbered=true]{hyperref} \usepackage{nameref} \usepackage{titlesec}

\begin{document}

\section{The first section}

\makeatletter \ttl@savetitle \makeatother

\section*{The second section}

\makeatletter \ttl@savetitle \makeatother

\end{document}

Werner
  • 603,163
  • Hi @Werner, thanks for your input! May I ask how to insert the command into a URL so that the current section title is retrieved automatically whenever the URL is opened? I played with \ttl@savetitle but did not succeed. – Carrie Sep 04 '23 at 02:44
  • @Carrie: With \makeatletter \href{http://google.com/\ttl@savetitle}{something2} \makeatother I get this output, which includes the title in the URL. – Werner Sep 04 '23 at 16:54
  • thanks for your reply! I applied it in my case and got two problems: 1. The document compiles only partially. LaTeX gives an error "TeX capacity exceeded, sorry [input stack size=5000], regarding @makeother #1->\catcode `#1 12\relax " 2. The \ttl@savetitle also retrieves subsection titles, but I only want section titles. – Carrie Sep 05 '23 at 02:20
  • @Carrie: Since my minimal example compiles and yours doesn't, you must be doing some different. Without something in-hand that replicates your issue, I can't do much. – Werner Sep 05 '23 at 17:15
  • Hi @Werner, thank you anyway! The URL in my case is indeed more complex. Besides, I won't provide a section title like {something2} in the URL, but just use a macro that can provide the current section title automatically depending on from which page the URL is opened. – Carrie Sep 08 '23 at 05:48