![\newenvironment 具有定义的颜色框](https://linux22.com/image/445285/%5Cnewenvironment%20%E5%85%B7%E6%9C%89%E5%AE%9A%E4%B9%89%E7%9A%84%E9%A2%9C%E8%89%B2%E6%A1%86.png)
抱歉,如果这个问题之前有人问过,但我找不到适合我的情况的答案。我想创建一个 \newenvironment,它会生成一个带有预定义颜色的框,里面可以有文本。我已经有一个它在 LaTeX 中应该是什么样子的例子:
使用这个代码:
\begin{center}
\definecolor{lightgray}{cmyk}{0,0,0,.04}
\setlength{\fboxsep}{10pt}\colorbox{lightgray}{\parbox{0.95\textwidth}{\blindtext}}
\end{center}
我曾尝试这样做:
\newenvironment{boksi}
{
\begin{center}
\definecolor{lightgray}{cmyk}{0,0,0,.04}
\setlength{\fboxsep}{10pt}\colorbox{lightgray}{\parbox{0.95\textwidth}}
}
{
\end{center}
}
由此产生了如下结果:
有谁可以告诉我我做错了什么和/或我应该做什么?
答案1
也许这个方法可以解决问题:
\documentclass{article}
\usepackage{blindtext}
\usepackage{xcolor}
\definecolor{lightgray}{cmyk}{0,0,0,.04}
\newbox\scratchbox
\newcommand\PassFirstToSecond[2]{#2{#1}}
\makeatletter
\newenvironment{boksi}{%
\expandafter\PassFirstToSecond\expandafter{\the\fboxsep}{%
\setlength{\fboxsep}{10pt}%
\begin{lrbox}{\scratchbox}%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep\relax}%-2\fboxrule\relax}%
% \centering
\setlength\fboxsep
}%
}{%
\end{minipage}%
\end{lrbox}%
\trivlist\item\relax\if@minipage\else\vskip\parskip\fi
% \fcolorbox{black}{lightgray}{\usebox{\scratchbox}}%
\colorbox{lightgray}{\usebox{\scratchbox}}%
\if@newlist\leavevmode\fi\endtrivlist
}%
\makeatother
\fboxsep=.5\fboxsep
\begin{document}
\noindent text \hfill text \hfill text \hfill text
\begin{boksi}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis \fbox{facilisis} sem. \blindtext
\end{boksi}
\noindent text \hfill text \hfill text \hfill text
\end{document}