我想水平偏移一个带框架的文本段落,使得框架的两侧与文本宽度为 6.5 英寸的页面上的左右边距 2em 精确重合。
缩进框架parbox
或是minipage
有问题的,因为所需的宽度会受到边距、文本宽度和固有的单位冲突的影响fboxsep
:
\hspace{2em}\fbox{\parbox{?}{Paragraph text}}
你有什么建议?
答案1
这里有两个提供类似布局的选项。framedparA
使用环境在所需宽度center
内水平设置内容。使用\parbox
framedparB
mdframed
并允许跨越页面边界进行分页。
\documentclass{article}
\usepackage{lipsum,environ,mdframed}
\NewEnviron{framedparA}[1][2em]{%
\begin{center}
\fbox{%
\parbox{\dimexpr\linewidth-#1-#1-2\fboxsep-2\fboxrule}{%
\strut\BODY\strut%
}%
}%
\end{center}
}
\NewEnviron{framedparB}[1][2em]{%
\begin{mdframed}[
leftmargin=#1,
rightmargin=#1,
innerleftmargin=\fboxsep,
innerrightmargin=\fboxsep
]
\BODY
\end{mdframed}
}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus molestie dui tellus,
vitae blandit quam scelerisque in. Ut tristique metus id magna commodo, nec semper sapien
efficitur. Sed pretium elit vitae mi tristique mollis. Nam nibh metus, dictum et turpis
sit amet, tincidunt vulputate sem.
\begin{framedparA}
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;
Etiam vel euismod massa, quis fermentum nisl. Aenean pellentesque tincidunt nibh, in
ornare mi malesuada ut. Sed imperdiet enim leo, at aliquam dolor finibus a. Duis odio
mi, molestie sed tortor efficitur, blandit molestie purus.
\end{framedparA}
Proin auctor risus et dui lobortis ultrices. Morbi et mauris id elit fringilla posuere.
Aenean laoreet lacus est, eu congue purus egestas nec. Sed non velit nulla. Cras vestibulum
ut massa quis commodo. Sed tempor, lorem a porta dictum, orci nulla scelerisque arcu, sed
lacinia nisi risus interdum velit. Duis nec erat ac urna pellentesque imperdiet.
\begin{framedparB}
Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;
Etiam vel euismod massa, quis fermentum nisl. Aenean pellentesque tincidunt nibh, in
ornare mi malesuada ut. Sed imperdiet enim leo, at aliquam dolor finibus a. Duis odio
mi, molestie sed tortor efficitur, blandit molestie purus.
\end{framedparB}
Proin auctor risus et dui lobortis ultrices. Morbi et mauris id elit fringilla posuere.
Aenean laoreet lacus est, eu congue purus egestas nec. Sed non velit nulla. Cras vestibulum
ut massa quis commodo. Sed tempor, lorem a porta dictum, orci nulla scelerisque arcu, sed
lacinia nisi risus interdum velit. Duis nec erat ac urna pellentesque imperdiet.
\end{document}
当然,mdframed
还有更多奇特的东西可以添加......
答案2
你的问题可以更清楚,但我认为你想要
\noindent
\hspace*{2em}%
\fbox{\parbox{\dimexpr\textwidth-4em-2\fboxsep-2\fboxrule\relax}%
{Paragraph text}}
或者可能更简单
\begin{center}
\fbox{\parbox{\dimexpr\textwidth-4em-2\fboxsep-2\fboxrule\relax}%
{Paragraph text}}
\end{center}
答案3
您可以使用tcolorbox
:
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{lipsum}
\newlength{\boxindent}
\AtBeginDocument{\setlength{\boxindent}{2em}}
\begin{document}
%% A ``ruler''
\noindent
\vrule width 2em height 0.4pt depth 0pt
\kern-.2pt
\vrule width .4pt height 3pt depth 2.6pt
\kern-.2pt
\vrule width \dimexpr\textwidth-2em\relax height 0.4pt depth 0pt
\begin{tcolorbox}[
left skip=\boxindent,
right skip=0pt,
width=\dimexpr\textwidth-\boxindent\relax
]
\lipsum[2]
\end{tcolorbox}
\end{document}
当然,您可以修改盒子的各个方面,并且最好定义一个新环境:
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{lipsum}
\newlength{\boxindent}
\AtBeginDocument{\setlength{\boxindent}{2em}}
\newtcolorbox{mybox}{
left skip=\boxindent,
right skip=0pt,
width=\dimexpr\textwidth-\boxindent\relax,
boxrule=0.4pt,
colframe=red!75!black,
colback=red!5!white,
sharp corners,
}
\begin{document}
%% A ``ruler''
\noindent
\vrule width 2em height 0.4pt depth 0pt
\kern-.2pt
\vrule width .4pt height 3pt depth 2.6pt
\kern-.2pt
\vrule width \dimexpr\textwidth-2em\relax height 0.4pt depth 0pt
\begin{tcolorbox}[
left skip=\boxindent,
right skip=0pt,
width=\dimexpr\textwidth-\boxindent\relax
]
\lipsum[2]
\end{tcolorbox}
\bigskip
\begin{mybox}
\lipsum[2]
\end{mybox}
\end{document}