如何绘制带有“外部填充”的框架?

如何绘制带有“外部填充”的框架?

我想在文本的某一部分周围画一个框,以便这部分文本周围的边距不受影响。

以下是借助 的解决方案tcolorbox。我通过不断尝试和修复找到了此解决方案。因此,我的问题是:这个解决方案好吗或者有更好的解决方案吗?我发现这个解决方案的唯一缺点是它没有考虑到 的值。这对我来说并不重要,因为我打算在等于 的\parindent文本中使用这段代码。\parindent0pt

tcolorbox更喜欢使用mdframed:tcolorbox似乎维护得更好。不过,任何解决方案都可能对我或未来的读者有趣。

谢谢

\documentclass{article}

\usepackage{showframe}
\usepackage{tcolorbox}

\def\exteriorPadding{3mm}
\newtcolorbox{myBox}
{size = minimal, colback = red, 
leftrule = -\exteriorPadding, left = \exteriorPadding,
rightrule = -\exteriorPadding, right = \exteriorPadding,
top = 2mm, bottom = 2mm}

\begin{document}

Test: This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.

\begin{myBox}
Test: This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.
\end{myBox}

\end{document}

在此处输入图片描述

答案1

您可以使用oversize选项,它可以放大边距,并且文本与非装箱段落一样宽。

\documentclass{article}

\usepackage{showframe}
\usepackage{tcolorbox}

\newtcolorbox{myBox}
{colback = red,  top=2mm, bottom=2mm, boxrule=0pt,
oversize}

\begin{document}

Test: This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.

\begin{myBox}
Test: This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.
\end{myBox}

\end{document}

在此处输入图片描述

答案2

在此处输入图片描述

\documentclass{article}


\usepackage{lipsum}
\usepackage[most]{tcolorbox}

\begin{document}
    

\texttt{With parindent=0pt}
    %\showthe\parindent  %%,---- find \parindent
    \begin{tcolorbox}[breakable,enhanced,before upper={\parindent0pt}]
        \lipsum[9]
    \end{tcolorbox}


\texttt{With parindent=15pt}
            \begin{tcolorbox}[breakable,enhanced,before upper={\parindent15pt}]
            \lipsum[9]
        \end{tcolorbox}
            \lipsum[8]
\end{document}

相关内容