一、预览效果

二、拟解决的问题
给Latex段落高亮的时候,一般会采用下面命令:
\usepackage{soul} % 用于文本荧光标记
\sethlcolor{yellow} % 设置高亮颜色为黄色
\hl{In this study} %段落中
当段落中没有其他命令的时候,可以正常高亮!

段内有命令时候的高亮报错,可当段落中出现了其他命令,比如参考文献、图、表格,则会出现各种问题,序号无法显示出来。
\hl{In this study, the analysis of Table \ref{tab2} and Figure \ref{fig:Fig3} demonstrates that the method proposed in \cite{bib35} exhibits significant effectiveness.}
表格处和图序号没有正常显示,参考文献[35]及之后都没能正常显示。
2.1 问题原因
使用 soul 宏包的 \hl{…} 来高亮一大段文本时,如果中间包含了 \cite、\ref、\footnote等命令,往往需要一些额外处理,否则就可能导致高亮“断开”或报错。这是因为 soul默认对这些命令的解析不完整,需要“注册”或手动保护它们。
2.2 解决方案——在导言区为 \cite 等命令“注册”
在开头加上\soulregister\cite7 、 \soulregister\ref7这类命令即可
soul 宏包提供了 \soulregister 命令,可以让 soul 正确识别某些命令,从而在高亮时避免被截断或报错。
正确代码如下所示:
\usepackage{soul} % 用于文本荧光标记
\sethlcolor{yellow} % 设置高亮颜色为黄色
\soulregister\cite7
\soulregister\ref7
\soulregister\cite7 的含义是:告诉 soul,\cite 这个命令包含 7 个字符(大多数情况下写 7就够了),并允许它在高亮时正常显示。
三、soul的完整使用
\usepackage{soul}
\usepackage{color, xcolor}
\soulregister{\cite}7 % 注册\cite命令
\soulregister{\citep}7 % 注册\citep命令
\soulregister{\citet}7 % 注册\citet命令
\soulregister{\ref}7 % 注册\ref命令
\soulregister{\pageref}7 % 注册\pageref命令
四、公式高亮
\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}
\newcommand{\mathcolorbox}[2]{\colorbox{#1}{$\displaystyle #2$}}
\begin{document}
For inline math, one can simply do \hl{colored $a=b$ math}. For display math, the following works:
\begin{equation}
\mathcolorbox{red}{y=\frac{x^2}{q}}+z
\end{equation}
\end{document}

另一种高亮方法:
\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}
\newcommand{\mathcolorbox}[2]{\colorbox{#1}{$\displaystyle #2$}}
\newcommand{\hlfancy}[2]{\sethlcolor{#1}\hl{#2}}
\begin{document}
For inline math, one can simply do \hl{colored $a=b$ math}. For display math, the following works:
\begin{equation}
\mathcolorbox{red}{y=\frac{x^2}{q}}+z
\end{equation}
And for the fancy version: \hlfancy{orange}{colored $a=b$ math}. Now, \hlfancy{green}{colored $a=b$ math}.
\end{document}
&spm=1001.2101.3001.5002&articleId=145352995&d=1&t=3&u=d38d99f3c48e4c9c94405b785f7907f4)
3783

被折叠的 条评论
为什么被折叠?



