I'm writing a URL that contains §ions= part. I would like to add a macro after §ions=, 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}

\sectionsbe in your example?? – Ulrike Fischer Aug 11 '23 at 08:38