\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}
我想像tcolorbox
环境一样与页边距对齐shaded
。我尝试过将设置width
为\linewidth
,但仍然不起作用。
答案1
grow to left by
和grow to right by
是帮助您满足要求的标签。
请尝试以下操作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}
答案2
模拟环境的水平和垂直方向、内外间距shaded
:
\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}