3

I want to draw a frame around a part of the text so that the margins around this part of the text are not affected.

Below is a solution with the help of tcolorbox. I've found this solution by trying-and-fixing. So, my question is: is this solution good or is there a better one? The only drawback to this solution I see is that it does not take into account the value of \parindent. This is not important for me as I intend to use this piece of code in a text where \parindent equals 0pt.

I prefer using tcolorbox than mdframed: tcolorbox seems to be better maintained. Nevertheless, any solution could be interesting for me or for future readers.

Thank you

\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}

enter image description here

2
  • please have a look at the answer below -- with and without parindent -- solution at --tex.stackexchange.com/questions/200632/… – js bibra Feb 26 at 14:59
  • 1
    I guess you just want to use the tcolorbox option oversize. Try \newtcolorbox{myBox}{oversize, colback = red, top = 2mm, bottom = 2mm}. For details, see the documentation. – benjamin Feb 26 at 15:06
3

You can use oversize option, it enlarges margins and the text is as wide as in unboxed paragraphs.

\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}

enter image description here

1

enter image description here

\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}
2
  • But this restricts the margins, which is exactly what the OP does not want. – campa Feb 26 at 15:06
  • was addressing the issue of parindent – js bibra Feb 26 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.