使用mdframed
我们可以选择退出四条边界线中的任何一条。但我想要的是,两条垂直线都会出现,但不是顶部和底部的线,而是需要在左上角有一条部分线,在右下角有另一条部分线。以下是我迄今为止尝试过的代码:
\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}
\begin{document}
\mdfdefinestyle{myboxstyle}{%
rightline=true,
innerleftmargin=10,
innerrightmargin=10,
linecolor=gray,
outerlinewidth=1.0mm,
topline=false,
rightline=true,
bottomline=false,
skipabove=\topsep,
skipbelow=\topsep
}
\begin{mdframed}[style=myboxstyle]
\lipsum[1]
\end{mdframed}
\end{document}
上面的内容产生了下面的图像,其中顶部和底部的线条是我未能得到的。
答案1
这是 tcolorbox 版本。只需2cm
根据自己的喜好进行调整即可。
\documentclass{article}
\usepackage[skins]{tcolorbox}
\newtcolorbox{mybox}{enhanced,sharp corners=all,colback=white,colframe=gray,toprule=0pt,bottomrule=0pt,leftrule=1pt,rightrule=1pt,overlay={
\draw[gray,line width=1pt] (frame.north west) -- ++(2cm,0pt);
\draw[gray,line width=1pt] (frame.south east) -- ++(-2cm,0pt);
}}
\usepackage{lipsum}
\begin{document}
\begin{mybox}
\lipsum[2]
\end{mybox}
\end{document}
更新:添加标题。若要增加标题和文本之间的距离,只需使用bottom=1pt
(或更多) 即可boxed title style
。
\documentclass{article}
\usepackage[skins]{tcolorbox}
\newtcolorbox{mybox}[1]{enhanced,sharp corners=all,colback=white,colframe=gray,toprule=0pt,bottomrule=0pt,leftrule=1pt,rightrule=1pt,overlay={
\draw[gray,line width=1pt] (frame.north west) -- ++(2cm,0pt);
\draw[gray,line width=1pt] (frame.south east) -- ++(-2cm,0pt);
},attach boxed title to top left,boxed title style={frame hidden,interior hidden},title={\color{black}#1}}
\usepackage{lipsum}
\begin{document}
\begin{mybox}{Test}
\lipsum[2]
\end{mybox}
\end{document}
更新 2:如果你有一个只有一行高的盒装标题,你可能会对这个技巧感兴趣:
\documentclass{article}
\usepackage[skins]{tcolorbox}
\newtcolorbox{mybox}[1]{enhanced,sharp corners=all,colback=white,colframe=gray,toprule=0pt,bottomrule=0pt,leftrule=1pt,rightrule=1pt,overlay={
\draw[gray,line width=1pt] (frame.north west) -- ++(2cm,0pt);
\draw[gray,line width=1pt] (frame.south east) -- ++(-2cm,0pt);
},attach boxed title to top left={yshift=-20pt},boxed title style={frame hidden,interior hidden},top=.75cm,title={\bfseries\color{black}#1}}
\usepackage{lipsum}
\begin{document}
\begin{mybox}{Test}
\lipsum[2]
\end{mybox}
\end{document}
更新 3:这是更改标题高度的“正确”方法。
\documentclass{article}
\usepackage[skins]{tcolorbox}
\newtcolorbox{mybox}[1]{enhanced,sharp corners=all,colback=white,colframe=gray,toprule=0pt,bottomrule=0pt,leftrule=1pt,rightrule=1pt,overlay={
\draw[gray,line width=1pt] (frame.north west) -- ++(2cm,0pt);
\draw[gray,line width=1pt] (frame.south east) -- ++(-2cm,0pt);
},
coltitle=black,colbacktitle=white,titlerule=0pt,
title={\vskip5pt\bfseries#1}
}
\usepackage{lipsum}
\begin{document}
\begin{mybox}{This is a very long title which seems pretty ridiculous, but is used, although it is nonsense}
\lipsum[2]
\end{mybox}
\end{document}