1

I am writing a document which contains a lot of half-integer indices.

In display mode, x_{i + \frac {1} {2}} looks fine.

However, in inline mode, having several x_{i + \frac {1} {2}} messes up the interline spacing. x_{i + 1/2} looks fine, though.

I would like a command that allows switching between both:
- x_{i + \frac {1} {2}} when in display mode,
- x_{i + 1/2} when in inline mode.

I tried playing around with the answer from this question, but could not get it to work properly in my case. Any ideas?

3
  • You may be able to do it by hooking into \everymath{..} and activating a bool, for instance. Or making _ active and hooking a bool in there.
    – Manuel
    May 12, 2016 at 14:03
  • Wouldn't it be easier to just type x_{i + 1/2} when you are in inline math? First, these are fewer letters to type than x_{i + \frac{1}{2}} and second there is no autodetection for inline math mode, which means you will very soon hit corner cases. May 12, 2016 at 14:26
  • @HenriMenke: This is of course possible. For the moment, I am using a command \iph to represent i + \frac {1} {2}. My question probably was not clear enough: I would like to replace the \frac in the previous expression by a command that automatically chooses which fraction expression to display. However, since there is no autodetection for inline math mode, it would be hard to get such a behavior...
    – Victor
    May 12, 2016 at 14:35

1 Answer 1

2

This is a solution, altough in LaTeX2e there's no real machinery around \everymath so you are using the primitive and it may interfere with other packages (I don't really know any example, but it's possible that there's).

\documentclass{scrartcl}
\usepackage{mathtools,amssymb}

\newif\ifinlinemath
\newcommand*\versatilefrac[3][]{\ifinlinemath{#2#1/#3}\else\frac{#2}{#3}\fi}
\everymath{\inlinemathtrue\everymath{}}

\begin{document}

$x_{i + \versatilefrac{1}{2}}$
\[
  x_{i + \versatilefrac{1}{2}}
\]

\end{document}
3
  • I'd use \mathchoice. Of course, then also fractions inside fractions in displaystyle would be converted to the slashed form. May 12, 2016 at 14:25
  • @Manuel: That seems to work, without conflicts so far! Thanks! However, it does not work in align environments. Is there a way to modify your command to work in such environments?
    – Victor
    May 12, 2016 at 14:39
  • @HenriMenke But how does \mathchoice now if it's a subscript in inline or display math?
    – Manuel
    May 12, 2016 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .