在文本周围创建框,但不创建段落且带换行符

在文本周围创建框,但不创建段落且带换行符

我想在一个或多个单词周围画一个方框(没有圆角,保持简单 ^^)。有两点需要注意:1)方框应该支持换行;2)方框不应该覆盖多行。我添加了一个图形以使我的要求更清晰。我测试了fbox它的所有派生类,但它们都为带框的单词创建了一个段落。谢谢!

示例

答案1

变体this answer突出显示与边距中的引号相关的任意文本块 - 这可以在 TeX/LaTeX 中完成吗?。框内的文字可以跨越多行,允许使用连字符,但不允许分页:

\documentclass{article}
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathmorphing}

% 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{0.4pt}

\colorlet{HLcolor}{black}
\sethlcolor{HLcolor!20}

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

\defhighlighter{HLcolor!20}{0}

\newcommand\highlight@DoHighlight{%
  \fill [outer sep = -15pt, inner sep = 0pt, decorate
        , 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

\newcommand\MarkText[1]{\highlight{#1}}

\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. \MarkText{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. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla. \MarkText{Donec varius orci} eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.

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. \MarkText{Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus.} Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris.

\end{document}

在此处输入图片描述

只需使用;可以使用(默认值= )\MarkText{text...}更改框架使用的规则的粗细。\LineWidth0.4pt

相关内容