6

Basically, I would like to something like \xspace does, i.e. the meaning of the macro should depend on what is following it. So for example if I say

\mymacro a

it might expand to foo, and if any other character is following, it should expand to bar. In my specific case I'll try to match a unicode character, if that makes a difference.

Edit: Specifically, I want to influence the spacing after an integral. When it stands alone, the default spacing is fine. If it is the first of multiple integrals, the space became too wide. Here is what I cobbled together from the previous answer:

\newcommand\timeintegral[1]{
    ∫_{t_0}^{t_1} \if ∫#1 \!\!\! \fi #1
}

This is how it looks in use:

\timeintegral E \, dt = \timeintegral ∫_z ∫_φ f \, dφ dz dt

enter image description here

Psirus
  • 5,835
  • How to manage Unicode characters depends on what engine you're using; with pdflatex it's different than with xelatex or lualatex. Knowledge of the character is needed; also a “real world” use case would be nice. – egreg Mar 26 '14 at 18:16
  • The previous answer (now deleted?) worked for me, so I'm good. I can still add the wanted information if you think this may be useful for someone else. – Psirus Mar 26 '14 at 18:34
  • I'm not really sure it worked, if you're using pdflatex and tested for a Unicode character beyond the ASCII block. – egreg Mar 26 '14 at 18:36
  • I'm using LuaLaTeX, and it works for what I wanted it to do. I'll add my usecase to the question. – Psirus Mar 26 '14 at 18:37
  • LuaLaTeX was the missing information! But, please, go ahead and show the use case. – egreg Mar 26 '14 at 18:38
  • @Psirus I undeleted my answer, however, it may not work for the case of unicode tests, since unicode chars are made of more than one byte, while \if is only going to test a byte. There are other tests that could capture it, I'm sure, but I'm not too keen on unicode testing. If the answer works for you, I'll leave it. If not, or if someone gives a better answer, I'll delete mine. – Steven B. Segletes Mar 26 '14 at 19:11

1 Answers1

4

This is a straightforward application of \@ifnextchar:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{TeX Gyre Pagella}
\setmathfont{Asana Math}

\makeatletter
\newcommand{\timeintegral}{%
   ∫_{t_0}^{t_1} \@ifnextchar∫{\!\!\!}{}%
}
\makeatother

\begin{document}
\[
\timeintegral E \, dt = \timeintegral ∫_z ∫_φ f \,dφ\,dz\,dt
\]
\end{document}

enter image description here

The LaTeX3 version:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{TeX Gyre Pagella}
\setmathfont{Asana Math}

\ExplSyntaxOn
\NewDocumentCommand{\timeintegral}{}
 {
  ∫\sb{t\sb{0}}^{t\sb{1}} \psirus_check_int:
 }
\cs_new_protected:Npn \psirus_check_int:
 {
  \peek_charcode_ignore_spaces:NT ∫ { \!\!\! }
 }
\ExplSyntaxOff

\begin{document}
\[
\timeintegral E \, dt = \timeintegral ∫_z ∫_φ f \,dφ\,dz\,dt
\]
\end{document}
egreg
  • 1,121,712