4

This question already has an answer here:

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

marked as duplicate by Henri Menke, Bobyandbob, Stefan Pinnow, Troy, CarLaTeX Nov 19 '17 at 7:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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

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.

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

  • 1
    Your answer on the duplicate was better: tex.stackexchange.com/a/169945/10995 – Henri Menke Nov 19 '17 at 3: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 4:27
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

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