GeneralFramed box with label vertically centered over top line

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
kostka
Posts: 11
Joined: Tue Sep 15, 2009 3:53 am

Framed box with label vertically centered over top line

Post by kostka »

Basically, on the top line of an fbox, I'd like the line to be interrupted by a label.

I've attached a pdf file that shows exactly what I'm after. Is there a way to do this? I'm not looking for someone to do the work for me, but some direction would be nice. I've tried using a tabular environment, but the vertical alignment doesn't work out. Might there be a way to "nudge" text to appear above where it otherwise "should" appear?

Thanks in advance.
Attachments
sample.pdf
(17.57 KiB) Downloaded 281 times
Last edited by kostka on Fri Apr 02, 2010 5:27 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

kostka
Posts: 11
Joined: Tue Sep 15, 2009 3:53 am

Re: Framed box with label vertically centered over top line

Post by kostka »

Found out about \raisebox, which might work for what I want. I'll post an update at some point.
kostka
Posts: 11
Joined: Tue Sep 15, 2009 3:53 am

Framed box with label vertically centered over top line

Post by kostka »

Yep, I was able to use /raisebox to do this, along with a bunch of other things. I'll post the solution here, but keep in mind it relies on several things in my own style file, including /ssp which sets spacing to single space.

What's really nice for me about this is the text within the environment is taken verbatim, so all special characters are okay.

Within the preamble, I have defined:

Code: Select all

% probably several others needed here as well
\usepackage{verbatim}
\usepackage{varwidth}

...

\newsavebox{\codebox}
\newsavebox{\labelbox}
\newenvironment{labelcodebox}[1]
{\VerbatimEnvironment\begin{lrbox}{\labelbox}\it\footnotesize#1\end{lrbox}%
\begin{lrbox}{\codebox}\begin{varwidth}{\linewidth-0.5in-2\tabcolsep-2\fboxrule}\ssp\small\begin{Verbatim}}
{\end{Verbatim}\end{varwidth}\end{lrbox}
{\begin{flushright}\ssp\tt\small\begin{tabularx}{\linewidth-0.5in}{|Xcp{1ex}<{\hfill}|}
\cline{1-1} \cline{3-3}
& \raisebox{1.3ex}[0ex]{\usebox{\labelbox}} & \\ [-2ex]
\multicolumn{3}{|@{}l@{}|}{
\begin{tabular}{@{}l@{}}
\setlength{\fboxrule}{0pt}\setlength{\fboxsep}{0.4\tabcolsep}\hspace{0.6\tabcolsep}\fbox{\usebox{\codebox}}
\end{tabular}
} \\
\hline
\end{tabularx}\end{flushright}}\par\ignorespacesafterend\noindent}
And then the code

Code: Select all

One can write
\begin{labelcodebox}{Sample Linux code}
> cd ..
> rm -r $OLDPWD
\end{labelcodebox}
to delete the directory you were just in.
Produces the output:
Image
Post Reply