标题与内容在同一行的块

标题与内容在同一行的块

我正在寻找一个块环境,其中标题与内容位于同一行。例如但可以为标题设置不同的背景颜色,以获得类似的效果。

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

在此处输入图片描述

答案1

这是一个tcolorbox基于的解决方案:

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

在此处输入图片描述

标题到第二行的垂直距离可以根据自己的喜好进行调整。我在代码中标记了按键:

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

如果将这两个尺寸放大,则红色和与下一行之间的距离在垂直方向上会有更多的空间。对于最后一个例子来说,这可能看起来更好,但(对我来说)对于第一个例子来说不是。如果需要,这些值可以配置。

答案2

我确信有这样的软件包,但只是为了好玩,我开发了一个 tikz 解决方案:

代码在这里:

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

该命令的行为与您要求的一样,但还有一个额外的可选参数,如果给定,则通过创建一个将环境主体向下放置一些 pt (或 cm 或其他)\rasebox。在最后一种情况下很有用(参见图像中的前一个)。

在此处输入图片描述

相关内容