3

I'm trying to write a command that will put symbols inside a square cap (see picture below). My attempt, |\overline{aaa}|, is far from good...

Enter image description here

boaz
  • 173
  • do you want all the "square caps" to be the same height (which would accommodate any letter), or can they just be high enough to accommodate the tallest letter under them? also, should the vertical stop at the baseilne, or should it go to the depth of whatever is covered (e;g, a "g" or a "y")? if you add that information to your question, you will get much better answers. – barbara beeton Nov 18 '17 at 21:25

3 Answers3

6

A solution with simple boxes and rules. The padding and the rule width can be configured with the same method as in \fbox by lengths \SquareCapSep and \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}

Result

TikZ

A solution with TikZ. It also allows for round line caps and line joins:

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

Result

If the vertical lines should end at the base line, then "south" should be replaced by "base":

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

Result

Heiko Oberdiek
  • 271,626
  • 1
    Your answer on the duplicate was better: https://tex.stackexchange.com/a/169945/10995 – Henri Menke Nov 19 '17 at 03:12
  • 1
    @HenriMenke Why should I repeat myself? ;-) In this answer, I have presented two different methods for text mode. The answer in the "duplicate" deals with math mode. – Heiko Oberdiek Nov 19 '17 at 04:27
3

A very simple method not relying on any packages is to just use 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}

enter image description here

By adjusting the argument of the \hspace* command you can decrease or increase the margins on the left and right.

3

Picture mode for getting round caps. With expl3 it's easy to get the height as a pure number (ratio with \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}

enter image description here

egreg
  • 1,121,712