3

I'm looking for a block environment with title on the same line as content. Something like this but with possibility to set a different background color for the title, in order to get something like that.

\begin{myblock}{Title.}
Blabla blabla blabla blabla blabla blabla blabla.
\end{myblock}

enter image description here

4

Here is a tcolorbox based solution:

\documentclass{article}

\usepackage[skins]{tcolorbox}

\newtcolorbox{myblock}[2][]{%
  enhanced,sharp corners,size=small,
  colframe=black,colback=blue!10!white,
  clip upper,% <- if outer box has rounded corners
  detach title,before upper={\tcbtitle\ },
  title={\tcbox[
    enhanced,size=minimal,frame hidden,
    top=1mm,enlarge top by=-1mm,
    left=2mm,enlarge left by=-2mm,
    bottom=0mm,enlarge bottom by=0.5mm,%<- change both to you liking
    right=1mm,
    fontupper=\bfseries,colback=red!50,coltext=red!50!black,
    on line]{\strut #2}},#1
}


\begin{document}

\begin{myblock}{Title.}
Blabla blabla blabla blabla blabla blabla blabla.
\end{myblock}

\begin{myblock}{Title.}
Blabla blabla blabla blabla blabla blabla blabla
blabla blabla blabla blabla blabla blabla
blabla blabla blabla blabla blabla blabla
blabla blabla blabla blabla blabla blabla
blabla blabla blabla blabla blabla blabla.
\end{myblock}

\begin{myblock}{Agy:}
Blabla blabla blabla blabla blabla blabla blabla
blabla blabla blabla blabla blabla blabla
blabla blabla blabla blabla blabla blabla
blabla blabla blabla blabla blabla blabla
blabla blabla blabla blabla blabla blabla.
\end{myblock}

\begin{myblock}{x}
Blabla blabla blabla blabla blabla blabla blabla.
\end{myblock}

\begin{myblock}{Test with $f(x)=\frac{3}{x}$:}
Blabla blabla blabla blabla blabla blabla blabla
blabla blabla blabla blabla blabla blabla
blabla blabla blabla blabla blabla blabla.
\end{myblock}

\end{document}

enter image description here

The vertical distance from the title to the second line may be adjusted to your liking. I marked the keys inside the code:

bottom=0mm,enlarge bottom by=0.5mm,%<- change both to you liking

If these two sizes are enlarged, there is more vertical room for the red color and the distanced to the following line. This may look nicer for the last example, but (for me) not for the first examples. If needed, these values could be made configurable.

2

I am sure there are packages for this, but just for fun I developed one tikz solution:

The code is here:

\documentclass{beamer}
\usepackage{tikz}
\usepackage{environ}

\newsavebox{\blockTitle}
\newsavebox{\blockContent}
\newlength{\TitleLength}
\newlength{\TitleHeight}
\newlength{\ContentLength}
\newlength{\ContentHeight}
\newlength{\extraSep}
\setlength{\extraSep}{3pt}
\NewEnviron{mblock}[2][0pt]{\par

  \noindent%
  \savebox{\blockTitle}{\hbox{#2}}%
  \savebox{\blockContent}{\parbox{\dimexpr\textwidth-2\extraSep}%
      {\hspace*{\dimexpr\wd\blockTitle+2\dimexpr \extraSep}\vphantom{\usebox{\blockTitle}}\ifdim #1=-1pt \relax\else\raisebox{0pt}[#1]{}\fi\BODY}}%
  \setlength\TitleLength{\wd\blockTitle}%
  \setlength\TitleHeight{\dimexpr\ht\blockTitle+\dp\blockTitle}%
  \setlength{\ContentLength}{\dimexpr\textwidth-2\extraSep}%
  \setlength{\ContentHeight}{\dimexpr\ht\blockContent+\dp\blockContent}%
  \begin{tikzpicture}
    \draw[fill=yellow] (0,0) rectangle (\dimexpr\ContentLength+\extraSep,-\ifdim\ContentHeight>\TitleHeight\dimexpr\ContentHeight+\extraSep\else\dimexpr\TitleHeight+\extraSep\fi) node[inner sep=0,outer sep=0,midway,] {\usebox{\blockContent}};
       \draw[fill=brown] (0,0) rectangle (\dimexpr\TitleLength+2\extraSep,-\dimexpr\TitleHeight+\extraSep) node[inner sep=0,outer sep=0,midway] {\usebox{\blockTitle}};
\end{tikzpicture}}
\begin{document}
\section{Test Section}
\begin{frame}

Test text over the blocks that will come after it and that splits in two lines
to let us check the text

\begin{mblock}{Test 1:}
  Here is a short text
\end{mblock}

\begin{mblock}{Test 2:}
  Here is a text that splits in two lines and the appearance has to behave
  ok with it!
\end{mblock}

\begin{mblock}{Test 2:}
  Here is a text and the function $F(x)=\frac{3}{x}$ that splits in two lines and the appearance has to behave
  ok with it! 
\end{mblock}



\begin{mblock}{Test with $f(x)=\dfrac{3}{x}$:}
  Here is a text and the new function $F(x)=\frac{3}{x}\cdot 1$ that splits in two lines and the appearance has to behave ok with it! {\color{red}This case is not nice and we can make it start from a new line like:}

\end{mblock}

\begin{mblock}[20pt]{Test with $f(x)=\dfrac{3}{x}$:}
  Here is a text and the new function $F(x)=\frac{3}{x}\cdot 1$ that splits in two lines and the appearance has to behave
  ok with it! 
\end{mblock}

\end{frame}
\end{document}

The command behaves like you asked but also has an extra optional argument that if given places the body of the environment down some pt s (or cm s or whatever) by creating a \rasebox. Usefull in the last case (see previous in the image).

enter image description here

  • 1
    +1, but it would be better to move \section{Test Section} outside the frame environment. – user36296 Sep 12 '18 at 20:05
  • Thanks @samcarter... Didnt noticed that... I always have it outside. I just started with article and lost that while changed – koleygr Sep 12 '18 at 20:26
  • Thanks for this homemade solution. Do you think there is a much easier solution using tcolorbox package ? – MB4E Sep 12 '18 at 20:39
  • @MB4E I don't use this kind of packages. Just don't accept to see if you will get other solutions. (See edited last case for same line) – koleygr Sep 12 '18 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.