如何在突出显示中添加斜体单词?

如何在突出显示中添加斜体单词?

我想突出显示文本,但其中的一些文本需要斜体。问题是,\hl{... \textit{...}...}导致里面的所有空格都\texit{...}被删除了。使用\可以解决问题,但这很累,很丑,生成的文本仍然有一些白色。另一个解决方案是使用\mbox{...},但对于长文本,它不会移动到下一行(.cls 文件禁止使用连字符来换行)。使用~也被删除了,毫无用处。不建议\textit{...}对每个单词单独使用,那将是一场噩梦。我只希望\textit{...}它的行为与在 外面完全相同\hl{}

\documentclass[12pt]{report}
\usepackage{ragged2e}

\usepackage{soul}
\usepackage{xcolor}
\sethlcolor{yellow}

\begin{document}

\hl{This is an \textit{example of sentence} that I want to highlight. This is an \textit{example of sentence} that I want to highlight. This is an \textit{example of sentence} that I want to highlight.}

\hl{This is an \emph{example of sentence} that I want to highlight. This is an \emph{example of sentence} that I want to highlight. This is an \emph{example of sentence} that I want to highlight.}

\hl{This is an \textit{\mbox{example of sentence}} that I want to highlight. This is an \textit{\mbox{example of sentence}} that I want to highlight. This is an \textit{\mbox{example of sentence}} that I want to highlight.}

\hl{This is an \textit{example\ of\ sentence} that I want to highlight. This is an \textit{example\ of\ sentence} that I want to highlight. This is an \textit{example\ of\ sentence} that I want to highlight.}

\hl{This is an \textit{example~of~sentence} that I want to highlight. This is an \textit{example~of~sentence} that I want to highlight. This is an \textit{example~of~sentence} that I want to highlight.}

\end{document}

笔记:

很多人说这\会导致错误,无法在 LaTeX 上编译。在使用上面的代码时,它编译成功了,但结果却出现了黑框。也许这就是我的引擎。有趣的是,它在我校园里的老同事创建的自定义 cls 下完美编译。我不是 LaTeX 专家,将尝试逐行删除使用包以找出哪些包删除了 textit 中的空格。感谢大家的善意。

注 2:发现它是 ragged2e。现在,如果可能的话,我不想删除这个包,因为我不知道会发生什么。我还更新了 MWE 以包含这个新包。

答案1

不要使用soul,而是soulpos使用包(其内部基于soulutf8,其基于soul,但soulpos使用其他方法来突出显示)。

我使用 also\emph而不是\textit,因为前者更具语义(我们使用斜体来强调)。

如果你仔细看,就会发现soul一些斜体字符被剪切掉了,并且在斜体文本后面添加了一点空白。

\documentclass{article}

\usepackage{xcolor}
\usepackage{soulpos}
%\sethlcolor{yellow} % by default, \hl highligh's color is yellow so I comment this line ; furthermore, \sethlcolor isn't available when only "soulpos" is called, but "\hl" is available, as this code shows (it's used in line 14 of this code)

\ulposdef{\myhl}{%
  \mbox{%
    \color{yellow}%
    \rule[-.8ex]{\ulwidth}{11pt}}}

\begin{document}

\hl{This is an \textit{example of sentence} that I want to highlight.} % "x" and "f" cut in the italic text, and white after "ce" 

\myhl{This is an \emph{example of sentence} that I want to highlight.} % all right
\end{document}

在此处输入图片描述

在这里您可以看到字符使用 剪切\hl,但没有使用\myhl(使用 自定义命令soulpos)。

在此处输入图片描述

\hl在这里,您可以看到由 创建但没有使用 的小空白\myhl

在此处输入图片描述

答案2

使用 LuaLaTeX,您可以更轻松地将lua-ul其用作包的替代品。soul

\documentclass{article}
%\usepackage{soul}
\usepackage[soul]{lua-ul}\usepackage{luacolor}
%\sethlcolor{yellow}

\begin{document}

\hl{This is an \textit{example\ of\ sentence} that I want to highlight.}
%\hl{This is an \textit{\mbox{example of sentence}} that I want to highlight.}
\hl{This is an \textit{example of sentence} that I want to highlight.}
\end{document}

答案3

希望你可以\emph使用\textit

\hl{This is an \emph{example of sentence} that I want to highlight.}

\hl{This is an \emph{\mbox{example of sentence}} that I want to highlight.}

\hl{This is an \emph{example~of~sentence} that I want to highlight.}

\hl标签内部,\不适用。

相关内容