如何将 Fcolorbox 与周围文本混合

如何将 Fcolorbox 与周围文本混合

基于\usepackage{xcolor}您可以使用命令框选文本\fcolorbox{declared-color-frame}{declared-color-background}{text}

可以使用以下 Gonzalo Medinas 代码创建带有彩色框架和换行符的彩色框绘制具有彩色背景和换行符的框,以适应文本宽度?(注:我将原来的改为\colorbox\fcolorbox为框架添加了颜色并添加了另一个框MyDBox。)

\usepackage{varwidth}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{lipsum}

\newcommand\MyCBox[1]{%
  \fcolorbox{blue}{SkyBlue}{\begin{varwidth}{\dimexpr\linewidth-2\fboxsep}#1\end{varwidth}}}

\newcommand\MyDBox[1]{%
  \fcolorbox{red}{RedOrange}{\begin{varwidth}{\dimexpr\linewidth-2\fboxsep}#1\end{varwidth}}}

我的问题:如何在文本周围创建一个彩色框,该框带有彩色框架,可以不间断地容纳“普通”文本,无论它是一个单词、多个单词还是几行单词。由于这个问题对我来说似乎不太清楚,我希望这两个示例之间的差异可以说明我的意思。

示例1

在此处输入图片描述

理想情况下,带有彩色边框的彩色框会像彩色文本 ( \textcolor{}{}) 一样平滑地融入其中。它不会超出默认的文本宽度,它可以以一行中的任何单词开头,也可以直接跟在中性文本或其他文本后面,等等\MyDBox

示例2

在此处输入图片描述

答案1

一个选项是使用soul包(我的回答的一个变体突出显示与边距中的引号相关的任意文本块 - 这可以在 TeX/LaTeX 中完成吗?):

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

\definecolor{HLcolor}{RGB}{124,18,18}
\sethlcolor{HLcolor!20}

\makeatletter
\newdimen\SOUL@dimen %new
\def\SOUL@ulunderline#1{{%
    \setbox\z@\hbox{#1}%
    \SOUL@dimen=\wd\z@ %new
    \dimen@i=\SOUL@uloverlap
    \advance\SOUL@dimen2\dimen@i %\dimen@ exchanged too
    \rlap{%
        \null
        \kern-\dimen@i
        \SOUL@ulcolor{\SOUL@ulleaders\hskip\SOUL@dimen}% new
    }%
    \unhcopy\z@
}}
\makeatother

\newcommand\MyDBox[2][HLcolor!20]{\sethlcolor{#1}\hl{#2}}

\begin{document}

Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. \MyDBox{Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla.} Donec varius orci eget risus \MyDBox[cyan!30]{Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum.} 

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum \MyDBox[orange!30]{gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.} Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. 

\end{document}

在此处输入图片描述

框架变化允许控制背景和框架的颜色:

\documentclass{article}
\usepackage{xparse}
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}

% The following code contains a variation of the great code by Antal S-Z
% in his answer to https://tex.stackexchange.com/a/6029/3954
%in TeX.SX

\newlength\LineWidth
\setlength\LineWidth{1.1pt}

\definecolor{HLcolor}{RGB}{124,18,18}
\sethlcolor{HLcolor}

\makeatletter
\newcommand{\defhighlighter}[3][]{%
  \tikzset{every highlighter/.style={draw=#2, fill opacity=#3, #1}}%
}

\defhighlighter[fill=olive!15]{HLcolor}{.5}

\newcommand{\highlight@DoHighlight}{
  \fill [outer sep = -15pt, inner sep = 0pt, every highlighter, this highlighter,draw=none]
        ($(begin highlight)+(0,8pt)$) rectangle ($(end highlight)+(0,-2pt)$) ;
  \draw[HLcolor,line width=\LineWidth]  ($(begin highlight)+(0,-2pt)$) -- ($(end highlight)+(0,-2pt)$) ;
  \draw[HLcolor,line width=\LineWidth]  ($(begin highlight)+(0,8pt)$) -- ($(end highlight)+(0,8pt)$) ;
}

\newcommand{\highlight@BeginHighlight}{
  \coordinate (begin highlight) at (0,0) ;
}

\newcommand{\highlight@EndHighlight}{
  \coordinate (end highlight) at (0,0) ;
}

\newdimen\highlight@previous
\newdimen\highlight@current

\DeclareRobustCommand*\highlight[1][]{%
  \tikzset{this highlighter/.style={#1}}%
  \SOUL@setup
  %
  \def\SOUL@preamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@BeginHighlight
      \draw[HLcolor,line width=\LineWidth]  ($(begin highlight)+(0,-2pt)+(0,-0.5\pgflinewidth)$) -- ($(begin highlight)+(0,8pt)+(0,0.5\pgflinewidth)$) ;
      \highlight@EndHighlight
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@postamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@EndHighlight
      \highlight@DoHighlight
      \draw[HLcolor,line width=\LineWidth]  ($(end highlight)+(0,-2pt)+(0,-0.5\pgflinewidth)$) -- ($(end highlight)+(0,8pt)+(0,0.5\pgflinewidth)$) ;
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@everyhyphen{%
    \discretionary{%
      \SOUL@setkern\SOUL@hyphkern
      \SOUL@sethyphenchar
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everyexhyphen##1{%
    \SOUL@setkern\SOUL@hyphkern
    \hbox{##1}%
    \discretionary{%
      \tikz[overlay, remember picture] \highlight@EndHighlight ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everysyllable{%
    \begin{tikzpicture}[overlay, remember picture]
      \path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
        \global\highlight@previous=\y0
        \global\highlight@current =\y1
      \endpgfextra (0,0) ;
      \ifdim\highlight@current < \highlight@previous
        \highlight@DoHighlight
        \highlight@BeginHighlight
      \fi
    \end{tikzpicture}%
    \the\SOUL@syllable
    \tikz[overlay, remember picture] \highlight@EndHighlight ;%
  }%
  \SOUL@
}
\makeatother

\DeclareDocumentCommand\MyDBox{O{HLcolor!15}O{HLcolor}m}{%
  \colorlet{HLcolor}{#2}
  \highlight[#1]{#3}%
}

\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. \MyDBox{Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.} Mauris ut leo. Cras viverra metus rhoncus sem. \MyDBox[yellow!15]{Nulla et lectus vestibulum urna fringilla ultrices.} Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. \MyDBox[cyan!20][cyan]{Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam.} Duis eget orci sit amet orci dignissim rutrum.

\end{document}

在此处输入图片描述

相关内容