1

I'm trying without sucess to make an environment like this:

enter image description here

What I have know is this:

enter image description here

The perfect answer to this question would be a way to have the title in colors and text out of the mdframed environment. The reason of this is because if you have to many text (or larges equations), mdframed goes crazy with "overfull \vbox". Part of my code is this:

\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}}

By the way, what I mean with "goes crazy" is this:

enter image description here

Thanks for read!

1
  • 1
    try with tcolorbox, it has much more options that mdframed. Oct 29, 2022 at 9:46

1 Answer 1

1

As Ulrike suggested, it can be easily done with 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}

enter image description here enter image description here

Edit:
Using a default title if the title is empty:

\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}

enter image description here

6
  • would be possible to have two titles? (like "if" conditions) One would be use when the solution environment doesn't have a title and the second would be the replacement of the first when the environment has a title. Do I explain myself?
    – Peluche
    Nov 13, 2022 at 14:40
  • @Peluche Do you like somewhat like a not mandatory title? If the title is present: "Solution: the given title", if it is not present: "Solution: the default title"?
    – CarLaTeX
    Nov 13, 2022 at 15:27
  • Yes, precisely that
    – Peluche
    Nov 13, 2022 at 16:01
  • 1
    @Peluche Look at my edit, I hope it's what you like.
    – CarLaTeX
    Nov 13, 2022 at 19:06
  • Yess, thanks you!!
    – Peluche
    Nov 13, 2022 at 23:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .