5

I would like to have a framebox around some paragraph titles. Something like the diagram below. I would like the framebox to have a background color (e.g. cyan), round corners, and shade if possible.

+-----------------------+
|   Paragraph one       | Paragraph text starts here. 
+-----------------------+

I have been searching over the internet but only found solutions to wrap the entire paragraph or the section title which occupies a single line.

2
  • Welcome to the site. Of course, it would help if you showed us some code that you have tried. But in addition, your intent is not fully clear. Does this box lay on a single line only? If not, in what manner do you expect wrapping around the box to occur? If it is always a single line, is it of fixed size or does it grow with the paragraph name? Is the label literally "Paragraph one", "Paragraph two" etc? What document class are you using? etc. etc. Mar 10, 2015 at 12:44
  • Hi Steven, the width of the box should be fixed. This mean it may lay across multiple lines if the paragraph name is long. In this case, I would like the text close wrap around the box. I would like it grow with the paragraph name. The document class is \documentclass[11pt,a4paper]{article}.
    – user48564
    Mar 10, 2015 at 13:31

3 Answers 3

4

Without any fancy colors.

\documentclass{article}
\usepackage{titlesec}
\newcommand{\mypara}[1]{\hspace*{-\fboxsep}\fbox{\theparagraph\hskip1em #1}}
\titleformat{\paragraph}[runin]
{\normalfont\normalsize\bfseries}{}{0em}{\mypara}

\titlespacing*{\paragraph}{0pt}{3.25ex plus 1ex minus .2ex}{0.5em}
\setcounter{secnumdepth}{5}

\begin{document}
\section{some section}
\subsection{some subsection}
\subsubsection{some subsubsection}
\paragraph{A test paragraph}
Some test text.


\end{document}

enter image description here

With colors and powers of tikz

\documentclass{article}
\usepackage{lipsum}
\usepackage{titlesec}
\usepackage{tikz}
\newcommand{\mypara}[1]{%
\begin{tikzpicture}[baseline=(a.base)]
 \node[draw=red,line width=0.5pt,rounded corners=1ex,inner sep=2pt,fill=cyan] (a) {#1};
\end{tikzpicture}
}

\titlespacing*{\paragraph}{0pt}{3.25ex plus 1ex minus .2ex}{0.5em}

\newcommand\bparagraph[1]{%
\titleformat{\paragraph}[runin]
{\normalfont\normalsize\bfseries}{}{0em}{\mypara}
\paragraph{#1}
\titleformat{\paragraph}[runin]
{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}
}

\begin{document}
\section{some section}
\subsection{some subsection}
\subsubsection{some subsubsection}
\bparagraph{A test paragraph}
\lipsum[1]
\paragraph{A test paragraph}
\lipsum[1]


\end{document}

enter image description here

3
  • Many thanks Harish. Any chance to create a new command such as bparagraph? I only want to frame some paragraph names but not all.
    – user48564
    Mar 10, 2015 at 13:35
  • @user48564 Please see the edit.
    – user11232
    Mar 10, 2015 at 14:25
  • Will \bparagraph receive a star as in \bcparagraph* to produce unnumbered framed titles in case secnumdepth has been increased? Mar 10, 2015 at 15:51
5

Using tcolorbox and defining two commands for regular and framed paragraphs which can be used as many times as required:

enter image description here

The code:

\documentclass{article}
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage[many]{tcolorbox}

\newcommand\framedparagraph[1]{%
  \tcbox[
    enhanced,
    colframe=cyan,
    box align=base,
    nobeforeafter,
    top=1pt,
    bottom=1pt,
    left=2pt,
    right=2pt,
    enlarge left by=-1.2mm,
    boxsep=0pt,
    interior style={top color=orange!20,bottom color=magenta!20}
  ]{\theparagraph\hskip1em#1}%
}

\newcommand\FramedPara{%
\titleformat{\paragraph}[runin]
  {\normalfont\normalsize\bfseries}{}{0em}{\framedparagraph}
}

\newcommand\RegularPara{%
\titleformat{\paragraph}[runin]
  {\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}
}

\setcounter{secnumdepth}{5}

\begin{document}
\section{some section}
\subsection{some subsection}
\subsubsection{some subsubsection}
\FramedPara
\paragraph{A test paragraph} 
\lipsum[2]
\RegularPara
\paragraph{A test paragraph}
\lipsum[4]

\end{document}

Since paragraph titles are in a "runin" style, the height and depth for the box were kept to a minimum to not disrupt the regular separation between lines.

2
  • Thanks Gonzalo. Any chance to remove the number before the paragraph name?
    – user48564
    Mar 10, 2015 at 14:49
  • @user48564 Sure. If it's just for some of them, use \paragraph*{test}. To remove numbering for all of them, delete the line \setcounter{secnumdepth}{5}. Mar 10, 2015 at 14:55
3

Here is a no-frills version of fixed width: \cparagraph[optional-fixed-width]{name}. EDITED to allow box to grow with size of paragraph name (but must remain less than one line width). REEDITED to handle \parskip.

EDITED to automatically handle paragraph names that extend up to two lines in length.

The MWE below shows two paragraphs of variable title width, and then two of fixed width; and finally one that extends to a second line.

\documentclass{article}
\usepackage{xcolor,tabto,lipsum}
\newlength\hbuffer
\setlength\hbuffer{6pt}
\newcommand\cparagraph[2][0pt]{%
  \setbox2=\hbox{\bfseries#2\kern\hbuffer}%
  \ifdim\wd2>\textwidth\relax%
    \wd2=\dimexpr\textwidth+\hbuffer\relax\relax%
    \def\mystrut{%
      \rule[\dimexpr-\ht\strutbox-\baselineskip\relax]{0pt}{2\baselineskip}}%
  \else%
    \let\mystrut\strut%
    \ifdim#1>0pt\relax\wd2=#1\relax\fi%
  \fi%
  \setbox4=\hbox{\mystrut}%
  \fboxsep=.5pt\noindent\rule{-.5\hbuffer}{0pt}\colorbox{red}{\colorbox{cyan!15}{%
% THE 2" BOX WIDTH AND THE THE 3.25ex \vspace MAY NEED TWEAKING
  \makebox[\wd2][l]{\mystrut}}}%
  \vspace{\dimexpr-\ht4-\dp4-3.25ex+\fboxsep-\parskip\relax}%
  \paragraph{#2}\tabto*{\dimexpr\wd2-.5\hbuffer+.66ex\relax}\allowbreak}
%\parskip 1ex
\begin{document}
\cparagraph{Paragraph one}
\lipsum[4] 
\cparagraph{My other paragraph}
\lipsum[4]
\cparagraph[2in]{Paragraph one}
\lipsum[4] 
\cparagraph[2in]{My other paragraph}
\lipsum[4]
\cparagraph{My other paragraph with a very very long name 
that extends well past one line}
\lipsum[4]
\end{document}

enter image description here

There are at least two potential flaws with this approach:

  1. For a paragraph name that ends near the end of a line, the first word of the subsequent text may lay on the same line, extending into the margin. It can be remedied by manually inserting a \\ following the close of the \cparagraph argument;

  2. The algorithm determines the width of the title by setting it in a box. If the actual length is just slightly larger than \textwidth, the algorithm will believe that it extends to two lines and prepare a 2-line colorbox accordingly; however, the inter-word spaces might conceivably be reduced allowing the title to fit on a single line.

2
  • Many thanks Steven. Any chance to make the box with round corners and shades?
    – user48564
    Mar 10, 2015 at 13:20
  • @user48564 Not with this approach. It will require tikz or tcolorbox type packages to accomplish that. Mar 10, 2015 at 13:21

You must log in to answer this question.

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