如何使用 parbox 突出显示一些内联文本?

如何使用 parbox 突出显示一些内联文本?

我有一些段落需要突出显示,以便稍后在我的 Latex 文档中进行编辑。为此,我使用以下代码

\newcommand{\hl}[1]{\noindent\colorbox{pink}{\parbox{\dimexpr\linewidth-2\fboxsep} {#1}}}

当有段落时,这个功能可以完美地完成工作。但是当我只需要突出显示段落中的一个单词时,我应该能够切换回

\colorbox{pink}{#1}

如何 实现 上述 这种 自动化\newcommand{\hl}.

xcolor请注意,除了某些原因 之外,我不想为此目的添加任何包。

梅威瑟:

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}
\newcommand{\hl}[1]{\noindent\colorbox{pink}{\parbox{\dimexpr\linewidth-2\fboxsep} {#1}}} %Highlighter. 
\begin{document}
\hl{\lipsum[2]}

\lipsum[1]

Some inline \hl{ text} need to be highlighted. 

\end{document}

答案1

这将测量参数的内容,如果内容宽度大于,\hl则使用:\parbox\linewidth

\documentclass{article}
\usepackage{xcolor}
\usepackage{lipsum}
\makeatletter
\newcommand{\hl}[1]{%
  \setbox\@tempboxa\hbox{#1}%
  \ifdim\wd\@tempboxa>\linewidth
    \noindent
    \colorbox{pink}{%
      \parbox{\dimexpr\linewidth-2\fboxsep}{#1}%
    }%
  \else
    \colorbox{pink}{#1}%
  \fi}%Highlighter.
\makeatother
\begin{document}
\hl{\lipsum[2]}

\lipsum[1]

Some inline \hl{text} need to be highlighted. 

Some other very, very \hl{longer inline text to demonstrate the command} that needs to be highlighted.

But note that this is a very limited solution. \TeX's paragraph building algorithm doesn't allow us to know how much of the line is left so, for example, this kind of thing happens: Some other very, very \hl{longer inline text to demonstrate the command} that needs to be highlighted.

\end{document}

在此处输入图片描述

答案2

我建议使用soul这个:

\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}

\sethlcolor{pink}

\begin{document}

\hl{Nam dui ligula, fringilla a, euismod sodales,
  sollicitudin vel, wisi. Morbi auctor lorem non justo. Nam lacus
  libero, pretium at, lobortis vitae, ultricies et, tellus. Donec
  aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna,
  vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit
  mollis. Suspendisse ut massa. Cras nec ante. Pellentesque a nulla.
  Cum sociis natoque penatibus et magnis dis parturient montes,
  nascetur ridiculus mus. Aliquam tincidunt urna. Nulla ullamcorper
  vestibulum turpis. Pellentesque cursus luctus mauris.}

Some inline \hl{text} need to be highlighted.

\end{document}

在此处输入图片描述

相关内容