2

I would like to format the backreferences in italics when the citation is called from a custom command (but not otherwise). I've been playing with both \backrefxxx and \backrefalt inside the custom command, but to no avail.

\documentclass[a4paper,oneside,12pt]{book}
\usepackage[backref=page]{hyperref}

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@book{companion,
  author={Frank Mittelbach and Michel Goossens},
  title={The \LaTeX{} Companion},
  year={2008},
  publisher={Addison-Wesley},
  isbn={0-201-36299-6},
}
\end{filecontents*}

\newcommand{\mycite}[1]{ %%% first version I tried
  \let\oldbackrefxxx\backrefxxx
  \renewcommand{\backrefxxx}[3]{\hyperlink{page.##1}{\textit{##1}}}

  \cite{#1}

  \let\backrefxxx\oldbackrefxxx %%%% If I comment this line, all backreferences are in italics; it I uncomment it, all are in normal font 
}

\renewcommand{\mycite}[1]{  %%% second version I tried, same results
  \let\oldbackref\backref
  \let\oldbackrefalt\backrefalt
  \renewcommand*{\backref}{}
  \renewcommand*{\backrefalt}[4]{\textit{\ifcase ##1 {}\or {\textit{##2}}\else {\textit{##2}}\fi}}

  \cite{#1}

  %\let\backref\oldbackref
  %\let\backrefalt\oldbackrefalt
}


\begin{document}
\cite{companion}      %% the backreference to page 1 should be in normal font
\newpage
\mycite{companion}    %% the backreference to page 2 should be in italics
\newpage
\cite{companion}      %% the backreference to page 3 should be in normal font again

\bibliographystyle{plain}
\bibliography{\jobname.bib}
\end{document}

I see that this means that the backreferences are formatted all at the moment they are typeset, not at the moment the citation command is called; and at the moment of typesetting, each of \backrefxxx and \backrefalt can have only one definition.

Is there any way to make some items in the list of backreferences have a different style than others?

2

You could try this:

\documentclass[a4paper,oneside,12pt]{book}
\usepackage[backref=page]{hyperref}
\usepackage{xpatch,expl3}

\makeatletter
\newcommand\mymarkpage{}
\xpatchcmd\Hy@backout{{\thepage}}{{\mymarkpage\thepage}}{}{\fail}
\xpatchcmd\Hy@backout{{\thepage}}{{\mymarkpage\thepage}}{}{\fail}
\makeatother

\ExplSyntaxOn
\renewcommand{\backrefxxx}[3]{
 \tl_if_head_eq_charcode:nNTF {#1}!
  {
    \hyperlink{page.\tl_tail:n{#1}}{{\textit{\tl_tail:n{#1}}}}
  }
  {
    \hyperlink{page.#1}{#1}
  }}
\ExplSyntaxOff


\newcommand{\mycite}[1]{%%% 
 {{\renewcommand\mymarkpage{!}%
  \cite{#1}}}}


\begin{document}

\cite{companion}      %% the backreference to page 1 should be in normal font
\newpage
\mycite{companion}    %% the backreference to page 2 should be in italics
\newpage
\cite{companion}      %% the backreference to page 3 should be in normal font again


\bibliographystyle{plain}

\bibliography{biblatex-examples}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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