我正在尝试创建这样的环境,但没有成功:
我所知道的是:
这个问题的完美答案是让标题有颜色,而文本脱离 mdframed 环境。这样做的原因是,如果您有太多文本(或大型方程式),mdframed 会因“overfull \vbox”而发疯。我的部分代码如下:
\documentclass[a4paper, 12pt]{article}
\usepackage[framemethod=TikZ]{mdframed}
\newcounter{solution}[section]
\renewcommand{\thesolution}{\arabic{section}.\arabic{solution}}
\newenvironment{solution}[1][]{%
\refstepcounter{solution}
% Always:
\mdfsetup{leftmargin = 0.25cm, rightmargin = 0cm,
innertopmargin = 0.25cm, linecolor = gray!50,
linewidth = 2pt, topline = true,
frametitleaboveskip = \dimexpr-\ht\strutbox\relax}
% 'if' condition (without title)
\ifstrempty{#1}%
{\mdfsetup{frametitle = {%
\tikz[baseline = (current bounding box.east),
outer sep = 0pt]
\node[anchor = east, rectangle, fill = gray!50]
{\strut Soluci\'on};}}
}
% 'else' condition (with title)
{\mdfsetup{frametitle = {%
\tikz[baseline = (current bounding box.east),
outer sep = 0pt]
\node[anchor = east, rectangle, fill= gray!50]
{\strut Soluci\'on: #1};}}
}
\begin{mdframed}[]\relax}{%
\end{mdframed}}
顺便说一下,我说的“发疯”是指:
感谢阅读!
答案1
正如 Ulrike 所建议的,可以轻松完成tcolorbox
:
\documentclass[a4paper, 12pt]{article}
\usepackage{mwe}%for testing purpose only
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}
\newtcolorbox{solution}[2][]{%
enhanced,breakable,
attach boxed title to top left,
left = 0.25cm, right = 0cm,
%frame hidden,
title={\strut Soluci\'on: #2},
colbacktitle=gray!50,
coltitle=black,fonttitle=\bfseries,
attach boxed title to top left={yshift=-\tcboxedtitleheight/2, xshift=4mm},
boxed title style={sharp corners,colframe=gray!50, boxsep=0pt},
top=\tcboxedtitleheight/2+2mm,
opacityframe=0, opacityback=0,
borderline horizontal={0.5mm}{0pt}{gray!50},
}
\begin{document}
\begin{solution}{Title of the solution}
Something something
\end{solution}
\begin{solution}{Another solution}
It is also breakable
For testing purpose only: \blindtext[4]
\end{solution}
\end{document}
编辑:
如果标题为空则使用默认标题:
\documentclass[a4paper, 12pt]{article}
\usepackage{mwe}%for testing purpose only
\usepackage{etoolbox}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}
\newtcolorbox{solution}[2][]{%
enhanced,breakable,
attach boxed title to top left,
left = 0.25cm, right = 0cm,
%frame hidden,
title={\strut Soluci\'on: \ifstrempty{#2}{Default title}{#2}},
colbacktitle=gray!50,
coltitle=black,fonttitle=\bfseries,
attach boxed title to top left={yshift=-\tcboxedtitleheight/2, xshift=4mm},
boxed title style={sharp corners,colframe=gray!50, boxsep=0pt},
top=\tcboxedtitleheight/2+2mm,
opacityframe=0, opacityback=0,
borderline horizontal={0.5mm}{0pt}{gray!50},
}
\begin{document}
\begin{solution}{Title of the solution}
Something something
\end{solution}
\begin{solution}{}
Something something
\end{solution}
\begin{solution}{Another solution}
It is also breakable
For testing purpose only: \blindtext[4]
\end{solution}
\end{document}