Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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}}
share|improve this answer

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}

Links to the table of contents, the bookmarks, and the header lines are removed. The full example file:

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

\pagestyle{headings}

\makeatletter
\newcommand*{\slink}[2]{%
  \begingroup
  \@ifstar{%
    \toks@{\endgroup#2*}%
    \slink@star{\texorpdfstring{\hyperlink{#1}}{}}%
  }{%
    \toks@{\endgroup#2}%
    \@ifnextchar[{%
      \slink@opt{\texorpdfstring{\hyperlink{#1}}{}}{#2}%
    }{%
      \slink@{\texorpdfstring{\hyperlink{#1}}{}}{#2}%
    }%
  }%
}
\newcommand*{\sref}[2]{%
  \begingroup
  \@ifstar{%
    \toks@{\endgroup#2*}%
    \slink@star{\texorpdfstring{\hyperref[{#1}]}{}}%
  }{%
    \toks@{\endgroup#2}%
    \@ifnextchar[{%
      \slink@opt{\texorpdfstring{\hyperref[{#1}]}{}}{#2}%
    }{%
      \slink@{\texorpdfstring{\hyperref[{#1}]}{}}{#2}%
    }%
  }%
}
\def\slink@star#1#2{%
  \the\toks@{#1{#2}}%
}
\def\slink@opt#1#2[#3]#4{%
  \the\toks@[{#3}]{#1{#4}}%
}
\def\slink@#1#2#3{%
  \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{Section with link to equation}
\sref{eq:abc}\section*{Star section}
\sref{eq:abc}\section[Toc entry]{Section with link}
\begin{equation}
a+b=c\label{eq:abc}
\end{equation}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.