如何将符号放在方帽内?

如何将符号放在方帽内?

我正在尝试编写一个命令,将符号放在方形帽内(见下图)。我的尝试,|\overline{aaa}|还不够好……

在此处输入图片描述

答案1

一种非常简单的方法,不依赖任何包,只需使用tabular

\documentclass[border=5mm]{standalone}
\newcommand{\Shelter}[1]{\begin{tabular}{|c|}\hline \hspace*{-1mm}#1\hspace*{-1mm}\end{tabular}}
\begin{document}
Hello \Shelter{World}, why are \Shelter{marmots} so cute?
\end{document}

在此处输入图片描述

通过调整命令的参数,\hspace*您可以减少或增加左边和右边的边距。

答案2

具有简单框和规则的解决方案。填充和规则宽度可以使用与\fbox长度\SquareCapSep和中相同的方法配置\SquareCapRule

\documentclass{article}

\newlength{\SquareCapSep}
\newlength{\SquareCapRule}
\setlength{\SquareCapSep}{\fboxsep}
\setlength{\SquareCapRule}{\fboxrule}
\newcommand*{\SquareCap}[1]{%
  \begingroup
    \sbox0{#1}%
    \leavevmode
    \hbox{%
      \vrule width\SquareCapRule
      \vbox{%
        \hrule height\SquareCapRule
        \kern\SquareCapSep
        \hbox{%
          \kern\SquareCapSep
          \box0\relax
          \kern\SquareCapSep
        }%
      }%
      \vrule width\SquareCapRule
    }%
  \endgroup
}

\begin{document}
  \SquareCap{aaa}, \SquareCap{ab}, \SquareCap{Aa}

  \setlength{\SquareCapSep}{.5\fboxsep}
  \SquareCap{aaa}, \SquareCap{ab}, \SquareCap{Aa}
\end{document}

结果

蒂克兹

TikZ 的解决方案。它还允许使用圆线帽和线连接:

\documentclass{article}
\usepackage{tikz}

\newlength{\SquareCapSep}
\newlength{\SquareCapRule}
\setlength{\SquareCapSep}{.5\fboxsep}
\setlength{\SquareCapRule}{\fboxrule}
\newcommand*{\SquareCap}[1]{%
  \tikz[
    baseline=(X.base),
    inner sep=0pt,
    line width=\SquareCapRule,
    line cap=round,
    line join=round,
  ]\draw
    node(X){#1}
    ([xshift=-\SquareCapSep]X.south west)
    -- ([xshift=-\SquareCapSep, yshift=\SquareCapSep]X.north west)
    -- ([xshift=\SquareCapSep, yshift=\SquareCapSep]X.north east)
    -- ([xshift=\SquareCapSep]X.south east)
  ;%
}
\begin{document}
  \SquareCap{aaa}, \SquareCap{ab}, \SquareCap{Aa}, \SquareCap{xy}
\end{document}

结果

如果垂直线应在基线处结束,则“南”应替换为“基”:

\documentclass{article}
\usepackage{tikz}

\newlength{\SquareCapSep}
\newlength{\SquareCapRule}
\setlength{\SquareCapSep}{.5\fboxsep}
\setlength{\SquareCapRule}{\fboxrule}
\newcommand*{\SquareCap}[1]{%
  \tikz[
    baseline=(X.base),
    inner sep=0pt,
    line width=\SquareCapRule,
    line cap=round,
    line join=round,
  ]\draw
    node(X){#1}
    ([xshift=-\SquareCapSep]X.base west)
    -- ([xshift=-\SquareCapSep, yshift=\SquareCapSep]X.north west)
    -- ([xshift=\SquareCapSep, yshift=\SquareCapSep]X.north east)
    -- ([xshift=\SquareCapSep]X.base east)
  ;%
}
\begin{document}
  \SquareCap{aaa}, \SquareCap{ab}, \SquareCap{Aa}, \SquareCap{xy}
\end{document}

结果

答案3

用于获取圆形帽的图片模式。使用expl3可以轻松获取纯数字形式的高度(与 的比率\unitlength)。

\documentclass{article}
\usepackage{pict2e,xparse}

\ExplSyntaxOn
\NewDocumentCommand{\ltrfbox}{m}
 {
  \hspace{1pt}
  \group_begin:
  \hbox_set:Nn \l_tmpa_box { \hspace{\fboxsep} \strut #1 \hspace{\fboxsep} }
  \dim_set:Nn \unitlength { \box_wd:N \l_tmpa_box }
  \dim_set:Nn \l_tmpa_dim { \box_ht:N \l_tmpa_box }
  \tl_set:Nx \l_tmpa_tl
   {
    \fp_eval:n { \dim_to_fp:n { \l_tmpa_dim } / \dim_to_fp:n { \unitlength } }
   }
  \begin{picture}(1,\l_tmpa_tl)
  \put(0,0) { \box_use:N \l_tmpa_box }
  \roundcap
  \polyline(0,0)(0,\l_tmpa_tl)(1,\l_tmpa_tl)(1,0)
  \end{picture}
  \group_end:
  \hspace{1pt}
 }
\ExplSyntaxOff

\begin{document}

x \ltrfbox{aaa}, \ltrfbox{ab}, \ltrfbox{Aa}, \ltrfbox{xy} z

\end{document}

在此处输入图片描述

相关内容