2

It is possible to overlap and left-align text by using \rlap{A}BCD; however, how to overlap A in the middle (exactly centered) of BCD?

5 Answers5

4

The default is exactly centered overlap, but fine adjustments may be made with the optional argument.

\documentclass{article}
\usepackage{stackengine}
\newcommand\clap[3][0pt]{\stackengine{0pt}{#3}{\kern#1#2}{O}{c}{F}{F}{L}}
\begin{document}
\clap{A}{BCD}

\clap[6pt]{A}{BCD}

\clap[-6pt]{A}{BCD}
\end{document}

enter image description here

2

I use \makebox in combination with the \widthof command from the calc package.

\documentclass{article}
\usepackage{calc}

\begin{document}

\begin{itemize}
\item ABCD
\item \makebox[0ex][l]{BCD}A
\item \makebox[1ex][l]{BCD}A
\item \makebox[2ex][l]{BCD}A
\item \makebox[3ex][l]{BCD}A
\item \makebox[4ex][l]{BCD}A
\end{itemize}

\begin{itemize}
\item ABCD
\item \makebox[\widthof{BCD}/2-\widthof{A}/2][l]{BCD}A
\item \frame{\makebox[\widthof{BCD}/2-\widthof{A}/2][l]{BCD}}A %for illustration
\end{itemize}

\end{document}

enter image description here

With \makebox[\widthof{BCD}/2-\widthof{A}/2][l]{BCD}A you get:

enter image description here

Side note: Remove the items that include \makebox and you get a MWE.

2

It's easy with \ooalign:

\documentclass{article}

\newcommand{\overlap}[2]{%
  \leavevmode\begingroup
  \vphantom{#1#2}%
  \ooalign{\hfil#1\hfil\cr\hfil#2\hfil\cr}%
  \endgroup
}

\begin{document}

\overlap{A}{BCD}

\textsf{\overlap{I}{XXX}}

\end{document}

enter image description here

egreg
  • 1,121,712
  • The letter X is not really symmetric, so the result may seem off. – egreg Feb 13 '18 at 17:53
  • +1. Better, of course. I would rename \overlap to something like \myOverlap or \EgregOverlap or \CustomOverlap to avoid the impression that it is a "standard" command. And I would have guessed that an x is symmetrical :). – Dr. Manuel Kuehner Feb 13 '18 at 17:55
  • This tex command solution is cool, but \ooalign is slightly less intuitive for beginners. – Kevin Dong Feb 13 '18 at 17:55
  • 1
    @Dr.ManuelKuehner Do you do that with every custom macro you define? Must destroy auto-complete in your editor. – cfr Feb 14 '18 at 03:22
  • Maybe I don't define as many custom macros as you :). I am not a programmer and maybe therefore I am a bit conservative in this regard. I get confused by there little things. It's just an idea and not criticism. I do not understand the argument with the autocomplete. I use Texmaker and do not add custom commands to autocomplete if that's what you mean. Thanks for replying. – Dr. Manuel Kuehner Feb 14 '18 at 07:13
2

Your main concern here is to place A and BCD in a box that has the same size/width. That way you can overlay them and achieve the alignment you're after.

To obtain similarly-sized boxes (width-wise), you can use eqparbox's \eqmakebox[<tag>][<align>]{<stuff>}:

enter image description here

\documentclass{article}

\usepackage{eqparbox}

\begin{document}

BCD

\makebox[0pt][l]{\eqmakebox[ABCD]{A}}% or \leavevmode\rlap{\eqmakebox[ABCD]{A}}%
\eqmakebox[ABCD]{BCD}

\end{document}

\eqmakebox places <stuff> with the same <tag> in a box that has the maximum width of all <stuff>. Individual box <align>ment can be specified (left, center/default, right).

Werner
  • 603,163
  • Since eqparbox uses a \label-\ref-like system, you'll have to compile twice with every change in the <stuff> that influences the maximum width associated with that <tag>. – Werner Feb 13 '18 at 18:14
  • +1 Never crossed eqparbox before (at least I don't remember). Will look into it. So far I normally use \makeboxand \phantom manually etc. Do you know if Scott Pakin is also a user here`He maintains a lot of packages on CTAN. – Dr. Manuel Kuehner Feb 13 '18 at 19:13
  • 1
    @Dr.ManuelKuehner: I haven't seen him around, no. You'll find him on comp.text.tex though. – Werner Feb 13 '18 at 19:18
2

The \stackinset command is perhaps simpler to use. Nor correction was necessary for this example, so I left the corresponding arguments empty:

\documentclass[12pt]{article}
\usepackage{stackengine}
\usepackage[svgnames]{xcolor}

\begin{document}

\Large\stackinset{c}{}{c}{}{\color{Tomato}A}{BCD}

\end{document} 

enter image description here

Bernard
  • 271,350