Get Charter digits in both modes; old style in text mode

fontsluatexxcharter

How do I get Charter with its "oldstyle" option in text mode, and with Charter characters (including digits) in math mode?

The proposed solution needs to work with LuaLaTeX (because some text characters are non-ASCII and I prefer to paste those non-ASCII characters into the source file, and save the source as UTF-8).

Edit: Just in case it's relevant, I use MiKTeX v.2.9. Do I need to upgrade in order to solve this problem?

I can get tantalisingly close but can't get everything working at once.

MWE 1:

\documentclass[12pt,a4paper]{article}

\usepackage{amsmath}
\usepackage{fontspec}
\setmainfont{XCharter} % see https://tex.stackexchange.com/a/205558
\usepackage[oldstyle]{xcharter}

% See https://tex.stackexchange.com/a/347574 for why this is needed. 
\AtBeginDocument{%
  \Umathcode`0="7 "0 `0
  \Umathcode`1="7 "0 `1
  \Umathcode`2="7 "0 `2
  \Umathcode`3="7 "0 `3
  \Umathcode`4="7 "0 `4
  \Umathcode`5="7 "0 `5
  \Umathcode`6="7 "0 `6
  \Umathcode`7="7 "0 `7
  \Umathcode`8="7 "0 `8
  \Umathcode`9="7 "0 `9
}

\usepackage[charter]{newtxmath}

\begin{document}

Digits in plain text: 48/96=135/270.

$a=48, b=96, c=135, d=270\implies a/b=c/d.$ $8=5+3=9-1=2\times4=2^3.$

\end{document}

Digits in text mode are just how I want. Moreover, letters in math mode are Charter italics, which I want. However, digits in math mode are Computer Modern, despite my attempt to use the code in this answer, which answered a similar question with regard to Libertine.

MWE 2

\documentclass[12pt,a4paper]{article}

\usepackage{amsmath}

\usepackage{fontspec}
\setmainfont{XCharter}[Numbers={OldStyle}] % see https://tex.stackexchange.com/a/205558
\usepackage[charter]{newtxmath}

% See https://tex.stackexchange.com/a/347574 for why this is needed. 
\AtBeginDocument{%
  \Umathcode`0="7 "0 `0
  \Umathcode`1="7 "0 `1
  \Umathcode`2="7 "0 `2
  \Umathcode`3="7 "0 `3
  \Umathcode`4="7 "0 `4
  \Umathcode`5="7 "0 `5
  \Umathcode`6="7 "0 `6
  \Umathcode`7="7 "0 `7
  \Umathcode`8="7 "0 `8
  \Umathcode`9="7 "0 `9
}

\begin{document}

Digits in plain text: 48/96=135/270.

$a=48, b=96, c=135, d=270\implies ab=cd.$ $8=5+3=9-1=2\times4=2^3.$

\end{document}

Now I attempt to get old-style digits in text mode by hacking \setmainfont rather than \usepackage{xcharter}. But this seems to pass the parameter oldstyleI (rather than oldstyle) to \usepackage{xcharter}. Anyway, the result is that text mode has old-style digits but with a small cap I for a 1. Good news is that digits in math mode are Charter, which is what I want.

It would be even nicer if both text and math modes could use old-style Charter digits. That way I could keep tables as array environments in math mode. Having to go to text mode for digits in tables entails replacing e.g. $\begin{array}{r} n \\ 1 \end{array}$ with \begin{tabular}{r} $n$ \\ 1 \end{tabular}.

Best Answer

Since you are using LuaLaTeX, you can load unicode-math instead of newtxmath, which has compatibility problems. Use the XCharter-Math font for math.

EDIT: In the comments, you requested the numeral 1 from [oldstyleI], which is character variant 01 in the OpenType version of XCharter. A revised version is below.

\documentclass[12pt,a4paper]{article}

\usepackage{unicode-math}

\defaultfontfeatures[XCharter]{ Numbers=OldStyle,
                                CharacterVariant=1, % Change the shape of 1 in text mode
                                UprightFont=*-Roman,
                                BoldFont=*-Bold,
                                ItalicFont=*-Italic,
                                SlantedFont=*-Slanted,
                                BoldItalicFont=*-BoldItalic,
                                BoldSlantedFont=*-BoldSlanted,
                                Extension=.otf }

\setmainfont{XCharter}
\setmathfont{XCharter-Math}

\begin{document}

Digits in plain text: 48/96=135/270.

$a=48, b=96, c=135, d=270\implies ab=cd.$ $8=5+3=9-1=2\times4=2^3.$

\end{document}

XCharter sample

In the unlikely event that you ever want old-style numbers in math mode too, you can add the command:

\setmathfont{XCharter-Roman.otf}[range={up/num},
                                 Numbers=OldStyle,
                                 CharacterVariant=01] % Change the shape of 1 in math mode

Here is a version that works in PDFLaTeX:

\documentclass[12pt,a4paper]{article}

\usepackage{amsmath}
\usepackage[charter]{mathdesign}
\usepackage[oldstyle]{xcharter}

\begin{document}

Digits in plain text: 48/96=135/270.

$a=48, b=96, c=135, d=270\implies ab=cd.$ $8=5+3=9-1=2\times4=2^3.$

\end{document}

I recommend that you use unicode-math and LuaLaTeX when you can, and PDFLaTeX with legacy 8-bit fonts when you have to. But, since you say the problem here was being unable to read Unicode, there is a good chance that your document would work if you saved it in NFC (normalized precomposed) UTF-8 encoding. PDFLaTeX is not able to understand Unicode combining characters, bur can handle any Unicode character that directly maps to an 8-bit input encoding.

Edit:

If you truly want to use an OpenType text font together with a legacy Type 1 math font, this should work:

\documentclass[12pt,a4paper]{article}

\usepackage{amsmath}
\usepackage[charter]{mathdesign}
\usepackage{fontspec}

\setmainfont{XCharter}[NFSSFamily=mdbch, Numbers=OldStyle]

\begin{document}

Digits in plain text: 48/96=135/270.

$a=48, b=96, c=135, d=270\implies ab=cd.$ $8=5+3=9-1=2\times4=2^3.$

\end{document}