如果使用 \colorbox 突出显示,文本不会换行到边距

如果使用 \colorbox 突出显示,文本不会换行到边距

在测试文件中,例如;

\documentclass{article}
\begin{document}

\begin{itemize}
    \item Short line of text
    \item Comparatively long line of text to illustrate that latex is wrapping my text to a preset margin when I have not highlighted it.
\end{itemize}

\end{document}

文本按照预设的边距定义换行。没问题。

但是,如果项目符号或后续子项目符号行中的文本用突出显示,\colorbox{colour}{text}则不会换行。我正在使用;

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[usenames, dvipsnames]{color}

修改文本颜色和我的自定义边距定义由;

\usepackage[margin=1in,includefoot,includehead]{geometry}

这是我希望执行的突出显示的一个示例。

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[usenames, dvipsnames]{color}

\usepackage[margin=1in,includefoot,includehead]{geometry}

\begin{document}

\begin{itemize}
    \item Short line of text
    \item \colorbox{yellow}{Comparatively long line of text to illustrate that latex is not wrapping my text to a preset margin when it is being highlighted.}
        \begin{itemize}
            \item \colorbox{yellow}{The problem persists with sub-bullet points too. I assume this is an issue with the coloring command and not my margin definitions.}
        \end{itemize}
\end{itemize}

\end{document}

如果我没有适当地解释的话,问题就是这样的;

突出显示时文本不换行

请注意,如果我在突出显示的部分之后继续在同一行书写,文本将按预期换行。

答案1

\colorbox不能环绕文本,除非使用额外的\parbox内部\colorbox。我提供了一个命令来应用\parbox并将可用空间减少两个\fboxsep空格,否则它将再次溢出到边距中。

我建议使用tcolorbox(虽然不太容易使用,但用途更广泛)

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[usenames, dvipsnames]{color}

\usepackage[most]{tcolorbox}

\tcbset{enhanced jigsaw, sharp corners,box align=center,boxrule=0pt}

% A special box for this
\newtcolorbox{highlightbox}[1][]{colback=yellow,enhanced jigsaw, sharp corners,box align=center,boxrule=0pt,boxsep=0pt,#1}

\usepackage[margin=1in,includefoot,includehead]{geometry}

\newcommand{\wrapcolorbox}[2][]{%
  \colorbox{#1}{\parbox{\dimexpr\linewidth-2\fboxsep}{#2}}
}

\begin{document}

\begin{itemize}
    \item Short line of text
    \item \wrapcolorbox[green]{Comparatively long line of text to illustrate that latex is not wrapping my text to a preset margin when it is being highlighted.}
    \item \begin{tcolorbox}[colback=yellow]
        Comparatively long line of text to illustrate that latex is not wrapping my text to a preset margin when it is being highlighted.
        \end{tcolorbox}
        \begin{itemize}
        \item \begin{highlightbox}
            The problem persists with sub-bullet points too. I assume this is an issue with the coloring command and not my margin definitions.
          \end{highlightbox}
       \item \wrapcolorbox[green]{Comparatively long line of text to illustrate that latex is not wrapping my text to a preset margin when it is being highlighted.}
        \end{itemize}
\end{itemize}

\end{document}

在此处输入图片描述

答案2

\item {\color{yellow}Comparatively long line of text to illustrate that latex is not wrapping my text to a preset margin when it is being highlighted.}

应在边缘处换行。

答案3

你可以使用\soul如下包这里

相关内容