2

Given a framed package box (or any other of your choice), how can I make the first line (with only one or two words) float on the border the box with a bit of white space on the left and the right sides?

I'm looking for the following effect:

enter image description here

2 Answers 2

4

You could try this using the tikz library. I presumed you wanted a fully lined box, but not sure?enter image description here

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}

\tikzstyle{background rectangle}=[thin,draw=black]
\begin{tikzpicture}[show background rectangle]

\node[align=justify, text width=\textwidth, inner sep=1em]{
Some sample text here thats in the box
};

\node[xshift=3ex, yshift=-0.7ex, overlay, fill=white, draw=white, above 
right] at (current bounding box.north west) {
\textit{Push}
};

\end{tikzpicture} 
\end{document}
2
  • I get an odd effect where the box(es) immediately after the first box are shifted a bit to the right.
    – Lev
    Apr 11, 2017 at 23:01
  • Mmm not sure, I tried putting 2 boxes underneath each other and they are not shifted to the right. Can you provide more details or better an example of what you did?
    – Tina
    Apr 11, 2017 at 23:08
4

Here is a version using tcolorbox to draw the box and place the title on the edge.

enter image description here

This is the source code:

\documentclass{article}

\usepackage[skins]{tcolorbox}

\newtcolorbox{myframe}[2][]{%
  enhanced,colback=white,colframe=black,coltitle=black,
  sharp corners,boxrule=0.4pt,
  fonttitle=\itshape,
  attach boxed title to top left={yshift=-0.3\baselineskip-0.4pt,xshift=2mm},
  boxed title style={tile,size=minimal,left=0.5mm,right=0.5mm,
    colback=white,before upper=\strut},
  title=#2,#1
}

\begin{document}

\begin{myframe}{Push}
  Some sample text here thats in the box.
\end{myframe}

\begin{myframe}{Pop}
  Some sample text. Some sample text. Some sample text. Some sample text.
  Some sample text. Some sample text. Some sample text. Some sample text.
  Some sample text. Some sample text. Some sample text. Some sample text.
\end{myframe}

\end{document}
1
  • Very cool and intuitive!
    – Lev
    Apr 12, 2017 at 14:52

You must log in to answer this question.

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