1

I want the block title width to automatically adjust to the length of the text. In the following MWE, I have changed the beamer template for the block environment and set the width of the block title to a fixed measure. However, I want that measure not to be fixed and to adjust to the title text length.

\documentclass{beamer} 
\usetheme{Dresden}
\setbeamercolor{block title}{fg=white,bg=blue}
\setbeamercolor{block body}{fg=black,bg=black!5}
\setbeamerfont{block title}{size=\normalsize,series=\bf}
\setbeamerfont{block body}{size=\normalsize}
\setbeamertemplate{block begin}{
    \begin{beamercolorbox}[rounded=true,wd=2cm]{block title}
        \usebeamerfont*{block title}\insertblocktitle
    \end{beamercolorbox}
    \vskip1pt
    \usebeamerfont{block body}
    \begin{beamercolorbox}[wd=1\linewidth,sep=1pt,leftskip=0.2cm,rightskip=0.2cm]{block body}\vskip1pt
}
\setbeamertemplate{block end}{
    \vskip6pt
    \end{beamercolorbox}
}
\begin{document}

\begin{frame}
\begin{block}{Short title}
some text here
\end{block}
\begin{block}{A bit more long title}
some text here
\end{block}
\end{frame}

\end{document}

1 Answer 1

0

You can use the tcolorbox package:

\documentclass{beamer} 

\usetheme{Dresden}

\usepackage[most]{tcolorbox}


\setbeamertemplate{block begin}{
    \usebeamercolor[fg]{block body}
    \usebeamercolor[fg]{block title}
    \usebeamercolor[bg]{block body}
    \usebeamercolor[bg]{block title}    
    \begin{tcolorbox}[
      title=\insertblocktitle,
      attach boxed title to top center={yshift=-2mm},
      enhanced,
      colback=structure.fg!75!black!10!,
      colbacktitle=structure.fg!75!black,
      frame empty, 
      ]
}
\setbeamertemplate{block end}{
    \end{tcolorbox}
}
\begin{document}

\begin{frame}
\begin{block}{Short title}
some text here
\end{block}
\begin{block}{A bit more long title}
some text here
\end{block}
\end{frame}

\end{document}

enter image description here

The code can be further simplified with the new tcolorbox inner theme (https://www.ctan.org/pkg/beamertheme-tcolorbox ):

\documentclass{beamer} 

\usetheme{Dresden}
\usecolortheme{orchid}

\useinnertheme[rounded]{tcolorbox}

\tcbset{
  attach boxed title to top center={yshift=-2mm},
  enhanced,
  bottom=1mm
}

\begin{document}

\begin{frame}
\begin{block}{Short title}
some text here
\end{block}
\begin{block}{A bit more long title}
some text here
\end{block}
\end{frame}

\end{document}

enter image description here

You must log in to answer this question.

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