我正在编写一个文档,经常需要将多段文字放在一个框内。我使用
\framebox{\begin{minipage}{0.95 \textwidth}
Here is how you solve this problem.
Here is more of the solution.
\end{minipage}}
我想通过定义一个环境来让我的工作更轻松,所以我尝试了:
\newenvironment{soln}
{
\framebox{\begin{minipage}{0.95 \textwidth}
}
{
\end{minipage}}
}
毫不奇怪,当我尝试使用此环境时,会出现“额外 } 或忘记 \endgroup”错误。正确的做法是什么?
答案1
framebox
宽度框的宽度等于文本宽度:
\documentclass{article}
\usepackage{lipsum}
\newsavebox\solbox
\makeatletter
\newenvironment{sol}%
{
\@parboxrestore%
\begin{lrbox}{\solbox}%
\begin{minipage}{\dimexpr\linewidth-2\fboxsep-2\fboxrule}%
}
{\end{minipage}\end{lrbox}
\framebox{\usebox\solbox}
}
\makeatother
\begin{document}
\begin{sol}
\lipsum[1][1-3]
\lipsum[1][4-6]
\end{sol}
\end{document}
如果您希望framebox
文本居中距离更窄,则可以使用以下解决方案:
\documentclass{article}
\usepackage{lipsum}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\newsavebox\solbox
\makeatletter
\newenvironment{sol}%
{
\@parboxrestore%
\begin{lrbox}{\solbox}%
\begin{minipage}{0.8\linewidth}
}
{\end{minipage}\end{lrbox}%
\begin{center}
\framebox{\usebox\solbox}
\end{center}
}
\makeatother
\begin{document}
\begin{sol}
\lipsum[1][1-3]
\lipsum[1][4-6]
\end{sol}
\end{document}
(红线表示文本边界)
答案2
\documentclass[a4paper]{report}
\usepackage{pdfrender,xcolor}
\usepackage{palatino}
\pdfrender{StrokeColor=black,TextRenderingMode=2,LineWidth=0.2pt}
\usepackage[most]{tcolorbox}
%\usepackage{array, tabularx}
%\usepackage{booktabs}
\newtcbtheorem[auto counter]{myBox}{Project}{separator sign={\ :}}{myBox}
\newlength\mylength
\settowidth\mylength{Contract Placing Authority}
\setlength\mylength{\dimexpr\mylength+\tabcolsep+\arrayrulewidth\relax}
\newlength\mylowerlength
\settowidth\mylowerlength{77 SSS 2020}
\setlength\mylowerlength{\dimexpr\mylowerlength+\tabcolsep+\arrayrulewidth\relax}
\begin{document}
\begin{myBox}[grow to right by=3cm,grow to left by=3cm,]{K9}{}
Assignment List -- Policy\\
Assignemnt List noting sheet\\
Cables from battery\\
CES\\
Assignment list -- forward for approval
\begin{myBox}{MAG 15}{}
21 items\\
\end{myBox}
\begin{myBox}{MAG 20}{}
Signature
\end{myBox}
\end{myBox}\newpage
\end{document}