[Tex/LaTex] How to make your own symbol when Detexify fails

math-modemath-operatorssymbols

My first visit to TeX.SX came when I was looking for a symbol for a twisted product:

enter image description here

I knew about Detexify and the Comprehensive LaTeX Symbol List, but I could not find the symbol there. I tried the construction that was obvious to me, namely \overset{\scriptstyle \sim}{\times}, but the \sim was much too high. I Googled, and found this solution by @Hendrik Vogt. Thus I learned about \smash.

Later I needed the same symbol in a subscript, ultimately learning about \mathchoice and \ooalign. Since then I have found that many questions on TeX.SX needed similar techniques. I though it would be a good idea to have a single question whose answers gave visitors with modest LaTeX skills general guidelines on constructing new symbols using LaTeX and related systems.

So, how do you make your own symbol when Detexify fails?

Best Answer

If it's really not in Detexify, check the Comprehensive LaTeX Symbol List to see if your symbol can be found in an existing package. Note, The Comprehensive List is long! Over 300 pages. But it is searchable, well-organized, and has a good table of contents and index.

If that doesn't help, it may be time to design your own symbol. It's probably best to give your new symbol a name so it can be used repeatedly and transported more easily into another document.

If your symbol will be used as an operator with limits (like an integral or summation), you should use the \DeclareMathOperator or \DeclareMathOperator* command. Both of these use the amsmath package. The unstarred version places sub- and superscript limits to the right of the operator; the starred version places limits above and below the operator when it is in displaystyle. To illustrate:

\DeclareMathOperator*{\squareop}{\square}
\DeclareMathOperator{\triangleop}{\bigtriangleup}

[Note that \square uses the amssymb package.]

Then the code

\[
\squareop_{n=1}^{\infty} a_n \qquad \triangleop_{n=1}^{\infty} a_n
\]

will produce the following output:

enter image description here

More information on \DeclareMathOperator can be found in this answer by @Andrew Swann.

If your symbol is not going to be used in that fashion, you should probably use \newcommand.

If your symbol is a math symbol: Is it a binary operator (such as + or \times)? A binary relation (such as < or \leq)? Or an ordinary math symbol (such as ! or \infty)? The spacing is different for each case. Compare the three versions for the symbol \times:

\newcommand{\reltimes}{\mathrel{\times}}
\newcommand{\bintimes}{\mathbin{\times}}
\newcommand{\chrtimes}{{\times}}

Then \noindent $a\reltimes b \newline a\bintimes b \newline a\chrtimes b$ will produce the output:

enter image description here

Note the extra set of curly braces in \chrtimes. If you remove them you'll get the same output as \mathbin{\times}, since \times is by default a binary operator. You can enclose most math symbols in {} to turn them into ordinary math symbols.

Typically, binary relations have slightly more space than binary operators, and significantly more than ordinary symbols. However, the spacing changes when these appear as sub- or superscripts. All three examples above will look like A_{a\times b} if placed in a subscript.

Many new symbols can be created by modifying or combining existing symbols. To rotate, scale or reflect existing symbols, use the graphicx or graphics package. Documentation is here. The commands are \rotatebox, \scalebox, \resizebox and \reflectbox.

For example, if you want a \cong symbol (≅), but with the tilde reversed, the \reflectbox command from graphicx can be used. The code

\newcommand{\backcong}{\mathrel{\reflectbox{$\cong$}}}

will produce the desired effect with the code $A\backcong B$.

enter image description here

If you try using this code in a subscript (for example, $X_{A\backcong B}$), the new symbol will not scale down as it should. This is resolved below below using \mathchoice.

To combine multiple symbols (math or text) the \ooalign command can be used. @egreg has a detailed explanation here. The basic idea is that \ooalign creates a one-column table, with all rows superimposed on one another, and no padding outside the column. Each row of the "table" ends with \cr. Entries can be centered in the column using \hfil.

For example, to produce

enter image description here

we superimpose a \circ symbol with a text T character. The command

\newcommand{\Tcirc}{\mathbin{%
    \ooalign{\hfil$\circ$\hfil\cr\hfil T\hfil\cr}%
    }}

together with $A\Tcirc B$ produces the output.

To make sure your symbol looks right whether it's displayed, inline, script or scriptscript, you can use \mathchoice. (Note mathpalette (explained here by @egreg and @Werner) can be used when the four versions are identical except for style.)

\mathchoice
  {<do this if called in \displaystyle>}
  {<do this if called in \textstyle>}
  {<do this if called in \scriptstyle>}
  {<do this if called in \scriptscriptstyle>}

The above code will produce the corresponding output for each of the four math styles.

To illustrate, here is a solution to the twisted product question that will adjust to scripts and scriptscripts.

\newcommand{\twprod}{\mathbin{\mathchoice%
    {\ooalign{\hfil\raisebox{1.15ex}{\mbox{$\scriptstyle\sim$}}\hfil\cr\hfil$\times$\hfil\cr}}%
    {\ooalign{\hfil\raisebox{1.15ex}{\mbox{$\scriptstyle\sim$}}\hfil\cr\hfil$\times$\hfil\cr}}%
    {\ooalign{\hfil\raisebox{.85ex}{\mbox{$\scriptscriptstyle\sim$}}\hfil\cr\hfil$\scriptstyle\times$\hfil\cr}}%
    {\ooalign{\hfil\raisebox{.65ex}{\scalebox{.8}{$\scriptscriptstyle\sim$}}\hfil\cr\hfil$\scriptscriptstyle\times$\hfil\cr}}%   
}}

enter image description here

S^2\twprod S^2 \quad F_{S^2\twprod S^2} \quad F_{K_{S^2\twprod S^2}}

I downsized the \sim in each style so it fit better over the \times.

Similar effects can be obtained using stackengine. Documentation is here.

If you can't create your symbol by combining or modifying others, you can design your symbol from scratch using tikz, together with the ideas above. Here is an example by @marmot.

Related Question