5

I would like to use a different font in inline-style math mode ($...$) and displayed equations (e.g., \begin{equation} ... \end{equation}). Currently, I set my fonts with

\setmainfont{Frutiger 45 Light}
\setmathrm{Cambria Math}
\setmathfont(Digits,Latin){Cambria Math}

but I would like to use Frutiger in mathmode as well so there is no break in font style for in-line math. I tried defining a new command

\newcommandx{\textvar}[1]{
{
\setmathrm{Frutiger 45 Light}
\setmathfont(Digits){Frutiger 45 Light}
#1
}
\setmathrm{Cambria Math}
\setmathfont(Digits,Latin){Cambria Math}
}

but \setmath* seem to work only in the preamble and not in the document itself.

Is there any way to do this?

Mico
  • 506,678
user3696412
  • 285
  • 3
  • 10
  • 2
    I'm not familiar with the fonts in question but in the general case this is a bad idea. If you say \begin{equation}E=mc^2\end{equation} where $E$ stands for energy you want the 2 'E' characters to match. In particular there are some notations in which sans-serif characters have a different meaning, aren't there? – Chris H Jun 11 '15 at 13:50
  • I know that this is the generall idea behind this behaviour, but if I write something like $2.8 \pm 0.5 \mu m$ in the middle of a line, it just looks better if it is in the same font as the rest, and it is pretty annoying that up to now I have to type 2.8 $\pm$ 0.5 $\mu$m to get what I want, and the \mu is still in the wrong font. – user3696412 Jun 11 '15 at 13:57
  • Where 'wrong' means consistent with all other equations in the document. – Ian Thompson Jun 11 '15 at 14:02
  • 1
    Ok, so we settled that what I try is not considered best practice and that there's a sensible idea behind the default behavior. Can we now return to the original question of how to do it instead of discussing why one should or should not try something like this? – user3696412 Jun 11 '15 at 14:05
  • Does it have to be Xe(La)TeX, or could you use Lua(La)TeX? – Mico Jun 11 '15 at 14:22
  • I'm not quite sure. I choose XeTex over the my usual pdftex because it can utilize system fonts, which I need in this case. Is this possible in LuaTex? – user3696412 Jun 11 '15 at 14:27
  • 1
    The tradition on this site is to point out bad choices in comments, and I think we're quite happy with that tradition, cf. the meta question on this. – Sverre Jun 11 '15 at 14:47
  • I'm not happy with that tradition. It's one thing if you provide an answer and explain why you disagree with the approach taken by the person asking the question, but another if you just criticize based on your own tastes without providing help: "You don't want to do what you're doing, do it my way." – JPi Jun 11 '15 at 15:19
  • I totally agree with JPi here. If someone does it like LaRiFaRi, thats totally fine, I sometimes do this myself. But just saying "That's bad" without answering the question is just wasting everybodies time in my opinion. – user3696412 Jun 11 '15 at 17:24
  • What you and @JPi are saying here is that if you don't have the necessary technical know-how to answer the OP's question, then you have no business writing a comment pointing out that the thing the OP is trying to achieve is (for whatever reason) not recommended. I for one would not want such a community where the general guideline is "be a wiz or be quiet". But let's not discuss this here. This belongs in Meta. – Sverre Jun 11 '15 at 19:36
  • No, that's not what I'm saying. I was saying that it's not helpful not to answer a question and just criticize because of your taste. Besides, it's often clearer to ask a somewhat different question than the precise one you want an answer to, which can lead to unhelpful "you don't want to do that" commentary. (Incidentally, I don't like different inline and display math fonts, either.) – JPi Jun 12 '15 at 00:44
  • @JPi In my book, comments informing me about bad choices and bad implementations are helpful. You feel differently. I see no use in debating this disagreement any further. – Sverre Jun 12 '15 at 11:03

2 Answers2

5

As we are fans of good typography, the discussion about best practises is always mandatory and important.

I want to second what Chris says above:

Please do not try that at home!

% arara: lualatex

\documentclass{article}
\usepackage{unicode-math}
\setmathfont[version=lm]{Latin Modern Math}
\setmathfont[version=asana]{Asana Math}

\begingroup
\catcode`\$=\active
\protected\gdef$#1${\mathversion{asana}\(#1\)\mathversion{lm}}
\endgroup
\AtBeginDocument{\catcode`\$=\active}

\begin{document}
    \mathversion{lm}
    \begin{equation}E=mc^2\end{equation} where $E$ stands for energy \begin{equation}E=mc^2\end{equation}
\end{document}

Edit:

In comment you mention that you want to keep the old behaviour. In this case you should (highly recommended) define a new command for such things:

\newcommand*{\myinlinemath}[1]{{\mathversion{asana}\ensuremath{#1}}}

Edit 2:

For your very example I would just recommend to use the siuntix package:

% arara: xelatex

\documentclass{article}
\usepackage{fontspec}
\setmainfont{linux libertine o}
\usepackage{siunitx}

\begin{document}
\begin{equation}E=mc^2\end{equation}

something like \SI{2.8 \pm 0.5}{\mu\metre} in the middle of a line

\sisetup{detect-all}
something like \SI{2.8 \pm 0.5}{\mu\metre} in the middle of a line
\end{document}

enter image description here

LaRiFaRi
  • 43,807
  • Thanks for the answer. As said above, I know this is not the way it's usually done. However, I keep getting the ! LaTeX Error: Can be used only in preamble. test §abc \pm 55§ error (I switched out the catcode so I still have both options, but I think that should not be the problem?) – user3696412 Jun 11 '15 at 14:30
  • +1 for answering the question while still agreeing with me! – Chris H Jun 11 '15 at 14:30
  • @user3696412 you are welcome. XeLaTeX can be used as well. If you prefer. Redefining the $$ command might not be stable. I would not recommend that. I do not know, how to set the math font globally. But I just edited to work with versions which is more efficient. – LaRiFaRi Jun 11 '15 at 14:39
  • 1
    @LaRiFaRi I recommend editing your answer to say \SI{2.8 \pm 0.5}{\micro\metre} or \SI{2.8 \pm 0.5}{\um}. These commands fit the spirit of siunitx better and should result in an upright mu symbol instead of the italic one in your answer currently. – RobotRaven Sep 27 '18 at 19:24
3

Here's a solution that uses the \everymath and \everydisplay primitives to switch between two pre-defined math versions -- "Asana Math" for inline math and "XITS Math" for displayed equations.

The code needs to be run under LuaLaTeX; for some reason, I get a "TeX capacity exceeded, sorry" error message if I try to compile it under XeLaTeX.

Caveat: The code has been tested with several display-style math environments, but definitely not with all conceivable ones.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{amsmath}

\usepackage[no-math]{fontspec}
\setmainfont{Myriad Pro}

\usepackage{unicode-math}
\setmathfont[version=XITS]{XITS Math}
\setmathfont[version=Asana]{Asana Math}
\mathversion{Asana}
\everymath{\mathversion{Asana}}
\everydisplay{\mathversion{XITS}}

\setlength\textwidth{7cm} % just for this example
\begin{document}
$E=mc^2$, where $E$ stands for energy \dots 
\begin{equation} E=mc^2  \end{equation}

$E=mc^2$, where $E$ stands for energy \dots 
\begin{gather} E=mc^2  \end{gather}
\end{document}
Mico
  • 506,678