Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have to typeset certain formulas, and the notation involves stretchable boxes, pretty much like \boxed{X} makes, but occasionally I need to add a hat below and/or above, as the picture shows:

A fancy box

(Symbols in this fancy box should be on the baseline.)

I usually find anything I want in "The Comprehensive LaTeX Symbol List", but this time I think it's too special. Any ideas how I can implement this?

Thank you for suggestions!

share|improve this question
add comment

4 Answers

up vote 9 down vote accepted

A solution using tikz with lots of configuration options:

  • above: draw hat above box
  • below: draw hat below box
  • rule: line thickness
  • sep: distance between contents and frame (inner sep/\fboxsep)
  • height: height of the hat
  • drawcolor: color of the lines/frame/hat
  • textcolor: color of the text/math inside the frame
  • mathstyle: math style, default is \displaystyle as in \boxed of amsmath
  • bold: make math bold (\boldmath)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{kvoptions}
\usepackage{kvsetkeys}

\makeatletter
\SetupKeyvalOptions{
  family=boxedhat,
  prefix=boxedhat@,
  setkeys=\kvsetkeys,
}
\newcommand*{\boxedhatsetup}{\kvsetkeys{boxedhat}}
\DeclareBoolOption[true]{above}
\DeclareBoolOption[true]{below}
\DeclareBoolOption[false]{bold}
\DeclareStringOption[\fboxsep]{sep}
\DeclareStringOption[\fboxrule]{rule}
\DeclareStringOption[1ex]{height}
\DeclareStringOption[\displaystyle]{mathstyle}
\DeclareStringOption[]{drawcolor}
\DeclareStringOption[.]{textcolor}

\newsavebox\boxedhat@box
\newcommand*{\boxedhat}[2][]{%
  \begingroup
    \boxedhatsetup{#1}%
    \tikz[baseline=(X.base)]
      \draw[draw={\boxedhat@drawcolor}]
        let
          \n{sep}={\boxedhat@sep},
          \n{rule}={\boxedhat@rule}
        in
          [line width=\n{rule}]
          node[
            rectangle,
            draw,
            inner sep=\n{sep},
            text={\boxedhat@textcolor},
          ] (X) {%
            \global\setbox\boxedhat@box=\hbox{%
              \begingroup % because of \boldmath and color
                \m@th
                \ifboxedhat@bold
                  \boldmath
                \fi
                $\boxedhat@mathstyle#2$%
              \endgroup
            }%
            \copy\boxedhat@box
          }
        \pgfextra{%
          \def\width{\wd\boxedhat@box}%
          \def\height{\ht\boxedhat@box}%
          \def\depth{\dp\boxedhat@box}%
          \def\totalheight{\dimexpr\ht\boxedhat@box+\dp\boxedhat@box\relax}%
        }%
        let
          \n{hatheight}={\boxedhat@height},
          \p{ul}=($(X.north west) + (.5*\n{rule},0)$),
          \p{uc}=(X.north),
          \p{ur}=($(X.north east) + (-.5*\n{rule},0)$),
          \p{top}=(\x{uc}, \y{uc} + \n{hatheight}),
          \p{ll}=($(X.south west) + (.5*\n{rule},0)$),
          \p{lc}=(X.south),
          \p{lr}=($(X.south east) + (-.5*\n{rule},0)$),
          \p{bottom}=(\x{lc}, \y{lc} - \n{hatheight})
        in
          \ifboxedhat@above
            (\p{ll}) -- (\p{ul}) -- (\p{top}) -- (\p{ur}) -- (\p{lr})
          \fi
          \ifboxedhat@below
            (\p{ul}) -- (\p{ll}) -- (\p{bottom}) -- (\p{lr}) -- (\p{ur})
          \fi
      ;%
  \endgroup
}
\makeatother

\boxedhatsetup{sep=.75\fboxsep}

\begin{document}
\[
  \boxedhat[above=false, drawcolor=blue]{x}
  < \boxedhat[rule=1pt,sep=1ex,bold,height=.5\height+1ex]{y'}
  = \boxedhat[textcolor=red]{\sum_{i=1}^{\infty}\frac{1}{n^2}}
  < \boxedhat[below=false, drawcolor=blue]{z}
\]
\end{document}

Result

share|improve this answer
    
Thank you very much! This looks great and that's what I had in mind. I used to draw everything in MetaPost, so I am not familiar with TikZ. –  user45435 Feb 5 at 10:28
add comment

Here's an option with tcolorbox; each one of the three commands \Hatup, \Hatdown, and \Hatupdown has an optional argument allowing you to use the features provided by the package to control the attributes:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tcolorbox}
\tcbuselibrary{most}
\usetikzlibrary{calc}

\makeatletter
\tcbset{
  up/.style={
    enhanced,
    boxrule=0.4pt,
    arc=0pt,
    outer arc=0pt,
    colframe=black,
    colback=white,
    boxsep=0pt,
    left=\fboxsep,
    right=\fboxsep,
    top=\fboxsep,
    bottom=\fboxsep,
    overlay={\draw[\kvtcb@colframe,line width=\kvtcb@left@rule] (frame.north west) -- ([yshift=1ex]$ (frame.north west)!0.5!(frame.north east) $ ) -- (frame.north east);}
  },
  down/.style={
    enhanced,
    boxrule=0.4pt,
    arc=0pt,
    outer arc=0pt,
    colframe=black,
    colback=white,
    boxsep=0pt,
    left=\fboxsep,
    right=\fboxsep,
    top=\fboxsep,
    bottom=\fboxsep,
    overlay={\draw[\kvtcb@colframe,line width=\kvtcb@left@rule] (frame.south west) -- ([yshift=-1ex]$ (frame.south west)!0.5!(frame.south east) $ ) -- (frame.south east);}
  },
  updown/.style={
    enhanced,
    boxrule=0.4pt,
    arc=0pt,
    outer arc=0pt,
    colframe=black,
    colback=white,
    boxsep=0pt,
    left=\fboxsep,
    right=\fboxsep,
    top=\fboxsep,
    bottom=\fboxsep,
    overlay={
      \draw[\kvtcb@colframe,line width=\kvtcb@left@rule] (frame.south west) -- ([yshift=-1ex]$ (frame.south west)!0.5!(frame.south east) $ ) -- (frame.south east);
      \draw[\kvtcb@colframe,line width=\kvtcb@left@rule] (frame.north west) -- ([yshift=1ex]$ (frame.north west)!0.5!(frame.north east) $ ) -- (frame.north east);}
  }
}
\makeatother

\newcommand\Hatup[2][]{\tcbhighmath[up,#1]{#2}}
\newcommand\Hatdown[2][]{\tcbhighmath[down,#1]{#2}}
\newcommand\Hatupdown[2][]{\tcbhighmath[updown,#1]{#2}}

\begin{document}

\[
a = \Hatup{b + c^{2}}\quad 
a = \Hatdown{b + c^{2}}\quad
a = \Hatupdown{b + c^{2}}
\]

\begin{align*}
\Hatup[colframe=red!80!black,boxrule=1pt]{\int x^{2}\,\textrm{d}x = \frac{1}{3} x^{3} + c} &= \Hatup{\sum_{i=i}^{n}a_{i}} 
\\[1ex]
\Hatupdown[coltext=orange]{\int x^{2}\,\textrm{d}x = \frac{1}{3} x^{3} + c} &= \Hatdown[colframe=cyan,boxrule=1pt]{\sum_{i=i}^{n}a_{i}}
\end{align*}

\end{document}

enter image description here

share|improve this answer
    
+1 That looks very nice :-) –  Thomas F. Sturm Feb 6 at 13:58
add comment

With PSTricks just for fun as usual.

\documentclass[preview,border=12pt]{standalone}
\usepackage{pst-node}

\psset{linewidth=\fboxrule}
\newcommand\mybox[2][4pt]{%
    \psDefBoxNodes{A}{\fbox{#2}}%
    \pspolygon(A:tl)([offset=#1]A:tC)(A:tr)(A:br)([offset=-#1]A:bC)(A:bl)}

\begin{document}
\mybox{Weirdstress Function}
\end{document}

enter image description here

Miscellaneous

Anything can be boxed.

\documentclass[preview,border=12pt]{standalone}
\usepackage{pst-node}

\psset{linewidth=\fboxrule}
\newcommand\mybox[2][4pt]{%
    \psDefBoxNodes{A}{\fbox{#2}}%
    \pspolygon(A:tl)([offset=#1]A:tC)(A:tr)(A:br)([offset=-#1]A:bC)(A:bl)}

\begin{document}
\mybox[6pt]{\parbox{4cm}{The Weirdstress function, \[\sum_{k=0}^\infty a^k \tan (b^k \pi x)\] in action.}}
\end{document}

enter image description here

share|improve this answer
    
+1 Is it possible to add math equations/symbols inside box like above examples –  texenthusiast Feb 5 at 4:38
    
@texenthusiast: yes of course, anything can be boxed. –  I am who I say I am Feb 5 at 4:39
    
In my opinion the requested top or bottom hat does not look aesthetically good no matter who creates the box. Sorry for this opinion and thank you for your cooperation. –  I am who I say I am Feb 5 at 7:00
add comment

I use stackengine to place the "hats" (I actually use triangles) above and below the item. I use \scalerel to stretch them. The length \trihite determines the height of the triangle hats.

\documentclass{article}
\usepackage{scalerel}
\usepackage{stackengine}
\stackMath
\renewcommand\useanchorwidth{T}
\usepackage{cmll}
\newlength\trihite
\setlength\trihite{8.8889pt}
\newsavebox\mybox
\newlength\boxwidth
\newcommand\underhat[1]{%
  \fboxrule .5pt%
  \sbox\mybox{\framebox{$#1$}}%
  \setlength\boxwidth{\wd\mybox}%
  \mathop{\stackunder[-2.2pt]{\framebox{$#1$}}{%
    \stretchto{\scaleto{\bigtriangledown}{1.15\boxwidth}}{\the\trihite}}}%
}
\newcommand\overhat[1]{%
  \fboxrule .5pt%
  \sbox\mybox{\framebox{$#1$}}%
  \setlength\boxwidth{\wd\mybox}%
  \mathop{\stackon[-2.3pt]{\framebox{$#1$}}{%
    \stretchto{\scaleto{\bigtriangleup}{1.15\boxwidth}}{\the\trihite}}}%
}
\newcommand\overunderhat[1]{%
  \fboxrule .5pt%
  \sbox\mybox{\framebox{$#1$}}%
  \setlength\boxwidth{\wd\mybox}%
  \mathop{\stackunder[-2.2pt]{%
    \stackon[-2.3pt]{\framebox{$#1$}}{%
      \stretchto{\scaleto{\bigtriangleup}{1.15\boxwidth}}{\the\trihite}}%
  }{%
      \stretchto{\scaleto{\bigtriangledown}{1.15\boxwidth}}{\the\trihite}}}%
}
\begin{document}
$\underhat{xxx} x \overhat{xxxxx} y \overunderhat{x}$
\end{document}

enter image description here

share|improve this answer
    
@GonzaloMedina Tweaked! –  Steven B. Segletes Feb 5 at 15:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.