9

I would like to put (any) two symbols on top of each other. With \overset the one on top is smaller. However, I want them to appear vertically centered and of equal size. How can this be done? Obviously, the following is quite a bad hack:

\documentclass{scrartcl}
\usepackage{amsmath}
\newcommand*{\ou}[2]{\overset{\text{\large ${#1}$}}{#2}}
\begin{document}
$a\ou{>}{<}0$ or $a\raisebox{-3pt}{\,$\ou{>}{<}$\,}0$ or $a\,{>\atop <}\,0$
\end{document}

\atop is not too bad, but a) how can I bring the symbols (vertically) closer to each other and b) how can this be defined as a command which takes care of the spacing around operators (\,)?

3 Answers3

5

Let me start by saying that the amssymb package offers you out of the box the symbol you need as \lessgtr (there's also \grtless).

If you need, for some reason, to build the symbol by stacking < and >, one option is to use \ooalign with some \raiseboxes; the whole construct can then be passed as argument to \mathrel to get the proper spacing associated to a relational symbol.

The following code shows a comparisson between your original attempts (first line), the one using \ooalign (second line), and the symbol obtained using \lessgtr (third line):

\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amssymb}

\newcommand*{\ou}[2]{\overset{\text{\large ${#1}$}}{#2}}

\newcommand\Ou{%
  \mathrel{{\ooalign{\hss\raisebox{-0.5ex}{$>$}\hss\cr\raisebox{0.5ex}{$<$}}}}
}

\begin{document}

$a\ou{>}{<}0$ or $a\raisebox{-3pt}{\,$\ou{>}{<}$\,}0$ or $a\,{>\atop <}\,0$

$a\Ou 0$

$a\lessgtr 0$

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • Hi Gonzalo, thanks for helping. Do you think this can be adapted to any symbols in math mode, not just > and <? – Marius Hofert Jun 22 '15 at 02:04
  • @MariusHofert Sure. The approach with \ooalign is very versatile and can be used for other symbols too. See http://tex.stackexchange.com/a/22375/3954 to learn more about \ooalign. – Gonzalo Medina Jun 22 '15 at 02:10
  • I tried to adapt it (with a 3rd argument for the space): \newcommand*{\ou}[3]{\mathrel{{\ooalign{\hss\raisebox{#3}{#1}\hss\cr\raisebox{-{#3}}{#2}}}}} and then called it with a\ou{>}{<}{0.5ex}0. Do you know how to get the 'negative sign' to work correctly? One could probably define a length and then set the corresponding negative length correctly, but that seems a bit tedious. – Marius Hofert Jun 22 '15 at 02:18
  • @MariusHofert this should work: \newcommand\ou[3]{\mathrel{{\ooalign{\hss\raisebox{-#3}{$#1$}\hss\cr\raisebox{#3}{$#2$}}}}} and then in your document $a\Ou{>}{<}{0.5ex} 0$. – Gonzalo Medina Jun 22 '15 at 02:24
  • @MariusHofert Of course, in my definition I assume that you want to stack math symbols (that's why I used $#1$ and $#2$). – Gonzalo Medina Jun 22 '15 at 02:26
  • Hi, using this, I obtain Illegal parameter number in definition of \ou. Does it work for you? – Marius Hofert Jun 22 '15 at 02:53
  • @MariusHofert Weird. If I copy paste the definition from my previous comment, the error you mentioned appears. However, if I write the exact same definition from scratch (whitout copy-paste from the comment), it works as expected. Perhaps in the copy-paste process some spurious characters get copied? Try writing the definition from scratch and let me know if it works. – Gonzalo Medina Jun 22 '15 at 02:57
  • excellent, not it works. There was a spurious space between the # and the 3 when it appeared for the second time. – Marius Hofert Jun 22 '15 at 03:03
  • Sorry, but \ooalign is the wrong tool for this. – egreg Jun 22 '15 at 06:46
  • @MariusHofert egreg has provided a new answer below, you should consider using and accepting his answer. – Gonzalo Medina Jun 22 '15 at 14:57
  • @egreg Don't be sorry :) – Gonzalo Medina Jun 22 '15 at 14:58
3

The symbol you want to build is already available, so this is just a hint for other similar situations. If you want to set symbols on top of one another, as opposed to superimpose symbols over each other, the tool to use is \ialign, not \ooalign that is handy for the latter case.

\documentclass{article}

\newcommand{\ou}{%
  \mathrel{%
    \vcenter{\offinterlineskip
      \ialign{##\cr$<$\cr\noalign{\kern-1.5pt}$>$\cr}%
    }%
  }%
}

\begin{document}
$a \ou b$
\end{document}

I use \mathrel to declare the symbol type, \vcenter to make the whole construction centered with respect to the formula axis and \ialign with no baselineskip, so I can fine tune the spacing with \noalign{\kern...}.

Improvements with \mathpalette are possible for making the symbol available in all formula styles.

enter image description here

egreg
  • 1,121,712
  • Thanks egreg. I just realized: if one of the symbols is much wider than the other, they will not be vertically aligned anymore. Is there a way to get them automatically vertically aligned? – Marius Hofert Jun 23 '15 at 02:43
  • @MariusHofert This much depends on the shapes of the symbols, so it's not possible to write code that does automatically what you want. – egreg Jun 23 '15 at 08:05
2

The stackengine package provides ready made macros for such things, providing options for vertical placement and separation, as well as horizontal alignment.

\documentclass{article}
\usepackage{stackengine}
\stackMath
\begin{document}
CENTERED STACK:

Specify gap:\def\stacktype{S} 
$x\mathbin{\stackanchor[0pt]{<}{>}}y\mathbin{\stackanchor[-1pt]{<}{>}}z$

Specify baselineskip:\def\stacktype{L} 
$x\mathbin{\stackanchor[6pt]{<}{>}}y\mathbin{\stackanchor[5pt]{<}{>}}z$\medskip

OVER STACK:

Specify gap:\def\stacktype{S} 
$x\mathbin{\stackon[0pt]{<}{>}}y\mathbin{\stackon[-1pt]{<}{>}}z$

Specify baselineskip:\def\stacktype{L} 
$x\mathbin{\stackon[6pt]{<}{>}}y\mathbin{\stackon[5pt]{<}{>}}z$\medskip

UNDER STACK:

Specify gap:\def\stacktype{S} 
$x\mathbin{\stackunder[0pt]{<}{>}}y\mathbin{\stackunder[-1pt]{<}{>}}z$

Specify baselineskip:\def\stacktype{L} 
$x\mathbin{\stackunder[6pt]{<}{>}}y\mathbin{\stackunder[5pt]{<}{>}}z$
\end{document}

enter image description here