Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to include some custom symbols in LaTeX. So I have some vector graphic images that I export to PDF, such as:

My Symbol

and I include them in LaTeX using
\newcommand*\mysymbol{\includegraphics{mysymbol.pdf}}.

I have two problems:

  1. The positioning in equations is off (it's above the line even though the image itself has no borders):

    enter image description here

  2. I cannot use it as a subscript (which I would like). For example, $H_\mysymbol$ throws the following error:

    ./mytex.tex:1000: Missing { inserted. 
    <to be read again> 
                        \let  
    l.1000 $H_\mysymbol
                       $
    ?
    
share|improve this question

4 Answers 4

up vote 13 down vote accepted

enter image description here

\documentclass[fleqn]{article}

\usepackage{graphicx}

\newcommand\xmysymbol{\raisebox{-.7ex}{\includegraphics[height=2.2ex]{mysymbol}}}
\newcommand\mysymbol{{\mathchoice{\xmysymbol}{\xmysymbol}{\hbox{\scriptsize\xmysymbol}}{\hbox{\tiny\xmysymbol}}}}

\begin{document}

\[\chi(x \in \mysymbol)\]

$\chi(x \in \mysymbol)$

$X_\mysymbol  + X_{x_\mysymbol}$

\end{document}
share|improve this answer

Just one trick instead of several. ;-)

\documentclass{article}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[export]{adjustbox}

\newcommand*{\mysymbol}{%
  \text{% change size in subscripts or superscripts
    \includegraphics[
      height=1.5ex,% adjust to suit
      valign=M,% center vertically
      raise=\fontdimen22\textfont2,% but raise it to the formula axis
    ]{YWWMA}}
}

\begin{document}
\[
\frac{1}{2}+\mysymbol-
H_\mysymbol \in \mysymbol^{\mysymbol^\mysymbol}
\]
\end{document}

enter image description here

share|improve this answer

Remarks:

  • The symbol is located at the base line. Probably you want to center it around the math axis (like +, ×, ...).

  • The height of the symbol is not defined. The example uses the height of H.

  • Subscripts and superscripts are not arguments, thus the rule that curly braces can be omitted, if there is only one token does not apply. It only works, if the token expands to a math symbol or subformula. The curly braces work, because they form a subformula.

  • The example uses \resizebox, because it is more flexible than options to \includegraphics. Then other stuff (tikzpicture, ...) can be used instead of \includegraphics.

  • The repeated use of \includegraphics can be avoided by using a box register. The image is stored in box register \mysymbolbox and reused in macro \mysymbol. This also avoids that the .log file is flooded with information data about the image mysymbol.

Full example:

\documentclass{article}
\usepackage{graphicx}

\newsavebox{\mysymbolbox}
\sbox{\mysymbolbox}{\includegraphics{myimage}}

\makeatletter
\newcommand*{\mysymbol}{%
  {% subformula in math that allows: H_\mysymbol
    \mathpalette{\@mysymbol}{}%
  }%
}
\newcommand*{\@mysymbol}[2]{%
  % #1: math style
  % #2: unused
  \settoheight{\dimen@}{$#1H$}% \dimen@ is a local scratch dimen register
  \vcenter{\hbox{% vertical centering around math axis
    \resizebox*{!}{\dimen@}{% total height = \dimen@
      \usebox{\mysymbolbox}%
    }%
  }}%
}
\makeatother

\begin{document}
\[
  H_\mysymbol \in \mysymbol^{\mysymbol^\mysymbol}
\]
\end{document}

Result

TikZ

Answers in the tag are not complete without TikZ (at least quite often):

\documentclass{article}
\usepackage{tikz}

\newsavebox\myimagebox
\sbox\myimagebox{% avoid space by line end
  \begin{tikzpicture}[
    x=1ex,
    y=1ex,
    line width=.7pt,% between semithick and thick
    line join=round,
  ]
    \draw (0,0) -- (1,-1) -- (3,1) -- (4,0) -- (3,-1) -- (1,1) -- cycle;
    \fill (0,0) -- (.5,-.5) -- (1.5,.5) -- (1,1) -- cycle;
  \end{tikzpicture}% avoid space by line end
}

\makeatletter
\newcommand*{\mysymbol}{%
  {% subformula in math that allows: H_\mysymbol
    \mathpalette{\@mysymbol}{}%
  }%
}
\newcommand*{\@mysymbol}[2]{%
  % #1: math style
  % #2: unused
  \settoheight{\dimen@}{$#1H$}% \dimen@ is a local scratch dimen register
  \vcenter{\hbox{% vertical centering around math axis
    \resizebox*{!}{\dimen@}{% total height = \dimen@
      \usebox\myimagebox
      % \includegraphics{mysymbol}%
    }%
  }}%
}
\makeatother

\begin{document}
\[
  H_\mysymbol \in \mysymbol^{\mysymbol^\mysymbol}
\]
\end{document}

Result TikZ

Variant

\documentclass{article}
\usepackage{amstext}
\usepackage{tikz}

\newcommand*{\mysymbol}{%
  \text{%
    \pgfmathsetmacro\mysymbolHeight{height("$H$")}%
    \pgfmathsetmacro\mysymbolLineWidth{\mysymbolHeight*.07}%
    \pgfmathsetmacro\MathAxis{height("$\vcenter{}$")}%
    \begin{tikzpicture}[
      x=\mysymbolHeight,
      y=\mysymbolHeight,
      line width=\mysymbolLineWidth,
      line join=round,
      baseline=-\MathAxis,
    ]
      \draw (0,0)--(.5,-.5)--(1.5,.5)--(2,0)--(1.5,-.5)--(.5,.5)--cycle;
      \fill (0,0)--(.25,-.25)--(.75,.25)--(.5,.5)--cycle;
    \end{tikzpicture}%
  }%
}

\begin{document}
\[
  H_\mysymbol \in \mysymbol^{\mysymbol^\mysymbol}
\]
\end{document}
share|improve this answer
\newcommand*\mysymbol{\raisebox{-1ex}{\includegraphics{mysymbol}}}

and

$H_{\mysymbol}$
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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