I am trying to use the memoir class to typeset a document in which the section headings function as hyperlinks to other locations in the document, using the hyperref package. One of the ways that \section and \hyperlink don't really cooperate is that I can't call \hyperlink within a \section (\section{\hyperlink{..}{...}} causes an error). To get around this, I found that I can get away with calling \section within the second argument of \hyperlink. This workaround, however, has the unexpected and unwanted consequence of throwing off the spacing for quotations within that section. I don't get the spacing between the bottom line of body text and the first line of quoted text that I would normally see. I looked at the hyperref and memoir documentation and sources, but to be honest the hyperref code is pretty much impenetrable for me and I could not find a solution. I can approximate one by manually line breaking at the end of the paragraph leading up to the quotation, but I would prefer a more elegant solution (and would like to understand what is going wrong).

Here is my MWE, which includes all of the packages I am calling. I am using XeLaTeX.

\PassOptionsToPackage{svgnames,x11names,hyperref}{xcolor}
\documentclass[article,12pt]{memoir}

\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{lettrine}
\usepackage{fixltx2e}
\usepackage{etex}
\usepackage{verbatim}
\usepackage{bookmark}
\usepackage{lipsum}
\usepackage{hyperref}

\pagestyle{empty}

\begin{document}

\mainmatter

\hyperlink{a}{\section{what i want in the section, but not in the quote}}
\lipsum[1]
\begin{quote}
\lipsum[2]
\end{quote}
\section{what i don't want in the section, but do in the quote}
\lipsum[3]
\begin{quote}
\lipsum[4]
\end{quote}
\end{document}


I understand that there are probably better ways to accomplish this task (\ref, etc.) But the targets and links are already set up, and I would very strongly prefer not to recode them, as it would take some creative regex find and replace. Which could easily go wrong.

-

This variant defines \slink and \sref. They take a name as argument and precede a sectioning command. \slink then converts the section title to a link to the given target name. \sref uses the name as label name. The star form and the optional argument of the sectioning command is supported, examples:

\slink{a}\section{what i want in the section, ...}...\hypertarget{a}{...}
\sref{foo}\subsection*{Section with star}...\label{foo}


\PassOptionsToPackage{svgnames,x11names,hyperref}{xcolor}
\documentclass[article,12pt]{memoir}

\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{lettrine}
\usepackage{fixltx2e}
\usepackage{etex}
\usepackage{verbatim}
\usepackage{bookmark}
\usepackage{lipsum}
\usepackage{hyperref}
\usepackage{bookmark}
\bookmarksetup{open,numbered}

\makeatletter
\begingroup
\@ifstar{%
\toks@{\endgroup#2*}%
}{%
\toks@{\endgroup#2}%
\@ifnextchar[{%
}{%
}%
}%
}
\newcommand*{\sref}[2]{%
\begingroup
\@ifstar{%
\toks@{\endgroup#2*}%
}{%
\toks@{\endgroup#2}%
\@ifnextchar[{%
}{%
}%
}%
}
\the\toks@{#1{#2}}%
}
\the\toks@[{#3}]{#1{#4}}%
}
\the\toks@[{#3}]{#1{#3}}%
}
\makeatother

\begin{document}

\frontmatter
\tableofcontents

\mainmatter

\slink{a}\section{what i want in the section, but not in the quote}
\lipsum[1]
\begin{quote}
\lipsum[2]
\end{quote}
\section{what i don't want in the section, but do in the quote}
\lipsum[3]
\hypertarget{a}{}
\begin{quote}
\lipsum[4]
\end{quote}

% more test cases
\newpage
\sref{eq:abc}\section*{Star section}
$$a+b=c\label{eq:abc}$$
\end{document}

-

Not sure if this is what you want (since you didn't provide a \hypertarget), but you can \protect the \hyperlink command and use \texorpdfstring:

\PassOptionsToPackage{svgnames,x11names,hyperref}{xcolor}
\documentclass[article,12pt]{memoir}

\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{lettrine}
\usepackage{fixltx2e}
\usepackage{etex}
\usepackage{verbatim}
\usepackage{bookmark}
\usepackage{lipsum}
\usepackage{hyperref}

\pagestyle{empty}

\begin{document}

\mainmatter

\section{\texorpdfstring{\protect\hyperlink{a}{what i want in the section, but not in the quote}}{what i want in the section, but not in the quote}}
\lipsum[1]
\begin{quote}
\lipsum[2]
\end{quote}
\section{what i don't want in the section, but do in the quote}
\lipsum[3]
\begin{quote}
\lipsum[4]
\end{quote}
\end{document}


And perhaps, you might also want to use the optional argument(s) to prevent the link from appearing in an eventual ToC (or headers):

\section[Text for the ToC]{\texorpdfstring{\protect\hyperlink{a}{what i want in the section, but not in the quote}}{what i want in the section, but not in the quote}}

-