2
\documentclass{article}
\usepackage{color,framed,tcolorbox}
\definecolor{shadecolor}{RGB}{241, 241, 255}

\begin{document}
    Shaded environment can align exactly with page margin: 
    \begin{shaded}
        This line is red
    \end{shaded}

    But tcolorbox can't: 
    \begin{tcolorbox}[colback=shadecolor,sharp corners=all,colframe=white!,width=\linewidth, boxrule=0pt, left skip=0pt]
    This line is red
    \end{tcolorbox}
\end{document}

I want to align tcolorbox with the page margin just like the shaded environment does. I've tried setting the width to \linewidth but it still doesn't work.

5
  • 1
    Note that the shaded box doesn't align with page margin rather exceeds it. The shaded* environment should be used if alignment with page margin is expected.
    – Imran
    Commented Jan 4, 2022 at 4:00
  • 4
    If you add \usepackage{showframe} (which will show the border of the page) to your premable you can see that tcolorbox align exactly to the border of the page accounting for the margin. Commented Jan 4, 2022 at 5:04
  • As per your MWE, tcolorbox works perfect, but the shaded exceeds the text width, please confirm that you need to get the colored box exceeds the text width by using tcolorbox?
    – MadyYuvi
    Commented Jan 4, 2022 at 11:26
  • 1
    @MadyYuvi Yeah, I want to make tcolorbox as wide as shaded.
    – Piko Mone
    Commented Jan 4, 2022 at 13:27
  • Similar: tex.stackexchange.com/q/585029 Commented Jan 6, 2022 at 1:03

2 Answers 2

1

grow to left by and grow to right by are the tags which helps you to meet the requirement.

Please try with the below MWE:

\documentclass{article}
\usepackage{color,framed,tcolorbox}
\definecolor{shadecolor}{RGB}{241, 241, 255}

\usepackage{showframe}
\tcbuselibrary{skins}
\begin{document}
    Shaded environment can align exactly with page margin: 
    \begin{shaded}
        This line is red
    \end{shaded}

    But tcolorbox can't: 
    \begin{tcolorbox}[grow to left by=12pt,grow to right by=12pt,colback=shadecolor,sharp corners=all,colframe=white!,boxrule=0pt]
    This line is red
    \end{tcolorbox}
\end{document}
1

To mimic both the horizontal and vertical, both the inner and outer spacings of shaded environment:

\documentclass{article}
\usepackage{color,framed,tcolorbox}
\definecolor{shadecolor}{RGB}{241, 241, 255}

\usepackage{showframe} % to show page frame(s)
\usepackage{lipsum}    % for \lipsum

\begin{document}
    \noindent\verb|shaded| env
    \begin{shaded}
        Environment content.\par
        \lipsum[1][1-3]
    \end{shaded}
    text after env
    
    \bigskip

    \noindent\verb|tcolorbox| env
    \begin{tcolorbox}[
      colback=shadecolor, sharp corners=all, colframe=white, boxrule=0pt,
      % horizontal spacing
      boxsep=9pt,                         % default value of \FrameSep
      left=0pt, right=0pt,
      oversize,
      % vertical spacing
      top=0pt, bottom=0pt,
      beforeafter skip balanced=\topsep,  % default value of \OuterFrameSep
      % restore \parindent
      before upper=\setlength{\parindent}{15pt},
    ]
        Environment content.\par
        \lipsum[1][1-3]
    \end{tcolorbox}
    text after env
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .