端に「タイトル」があり、周囲に空白があるボックス

端に「タイトル」があり、周囲に空白があるボックス

パッケージ ボックス (または任意の他のボックス)がある場合framed、最初の行 (1 行または 2 行の単語のみ) をボックスの境界上に浮かせて、左側と右側に少しの空白を残すにはどうすればよいでしょうか。

次のような効果を期待しています。

ここに画像の説明を入力してください

答え1

ライブラリを使用してこれを試すことができますtikz。完全に裏地付きのボックスが必要だと思いますが、確信はありませんか?ここに画像の説明を入力してください

\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

tcolorbox以下は、ボックスを描画し、その端にタイトルを配置するバージョンです。

ここに画像の説明を入力してください

これはソースコードです:

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

関連情報