制作 \boxed 的变体

制作 \boxed 的变体

我必须排版某些公式,并且符号涉及可拉伸的框,非常像\boxed{X}品牌,但有时我需要在下面和/或上面添加一顶帽子,如图所示:

一个精美的盒子

(这个花式方框内的符号应该位于基线上。)

我通常在“综合 LaTeX 符号列表”中找到我想要的任何东西,但这次我觉得它太特别了。有什么想法可以实现它吗?

感谢您的建议!

答案1

tikz使用大量配置选项的解决方案:

  • above:在盒子上方画帽子
  • below:在方框下方画帽子
  • rule: 线的粗细
  • sep:内容与框架之间的距离 ( inner sep/ \fboxsep)
  • height:帽子的高度
  • drawcolor:线条/框架/帽子的颜色
  • textcolor:框架内的文本/数学颜色
  • mathstyle:数学样式,默认\displaystyle\boxedamsmath
  • 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}

结果

答案2

这是一个选项tcolorbox\Hatup;这三个命令、\Hatdown和中的每一个\Hatupdown都有一个可选参数,允许您使用包提供的功能来控制属性:

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

在此处输入图片描述

答案3

像往常一样,使用 PSTricks 只是为了好玩。

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

在此处输入图片描述

各种各样的

任何东西都可以装箱。

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

在此处输入图片描述

答案4

我习惯stackengine将“帽子”(我实际上使用三角形)放置在物品的上方和下方。我习惯\scalerel拉伸它们。长度\trihite决定了三角形帽子的高度。

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

在此处输入图片描述

相关内容