【已解决】Latex中高亮段内命令(如参考文献引用、图、表格)

一、预览效果

在这里插入图片描述

二、拟解决的问题

给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}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旅途中的宽~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值