简体   繁体   中英

How can I highlight `\cite` (linebreak preserving) in latex

How can I highlight (set a background color) \cite (linebreak preserving) like this: [1]

I got a compile error when I tried \hl in the package soul like:

\hl{\cite{xxx}}

\mobx can fix this but doesn't support line break.

I also tried \colorbox and \newcommand\hl{\bgroup\markoverwith {\textcolor{GhostWhite}{\rule[-.5ex]{2pt}{2.5ex}}}\ULon} , but they do not support line break either.

Compile error example

main.tex:

\documentclass{article}
\usepackage{soul}
\begin{document}

\hl{{\cite{c1, c2, c3}}}

\bibliographystyle{plain}
\bibliography{ref} 
\end{document}

ref.bib:

@book{c1,
...
}
@book{c2,
...
}
@book{c3,
...
}

My QUESTION

  • What is the right way to highlight \cite

Wrap the \cite macro into an additional group of curly brackets:

\documentclass{article}

\begin{filecontents*}[overwrite]{\jobname.bib}
@book{knuth,
  author       = {Knuth, Donald E.},
  title        = {The {\TeX} book},
  date         = 1984,
  maintitle    = {Computers \& Typesetting},
  volume       = {A},
  publisher    = {Addison-Wesley},
  location     = {Reading, Mass.},
  langid       = {english},
  langidopts   = {variant=american},
  sortyear     = {1984-1},
  sorttitle    = {Computers & Typesetting A},
  indexsorttitle= {The TeXbook},
  indextitle   = {\protect\TeX book, The},
  shorttitle   = {\TeX book}
}
\end{filecontents*}

\usepackage{soul}

\begin{document}

\hl{{\cite{knuth}}}

\bibliographystyle{plain}
\bibliography{\jobname} 

\end{document}

For multiple citation, one could either wrap them in an \mbox{...} (this will lose the ability to have line breaks), or use lualatex instead:

% !TeX TS-program = lualatex
\documentclass{article}
\usepackage{lua-ul,luacolor}

\begin{filecontents*}[overwrite]{\jobname.bib}
@book{c1,
...
}
@book{c2,
...
}
@book{c3,
...
}
\end{filecontents*}

\begin{document}

\underLine{\cite{c1, c2, c3}}

\highLight{\cite{c1, c2, c3}}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM