1

The source below, when processed with pdflatex produces the math symbol shown here:

Unit square symbol

But if I process the source with xelatex, I get error:

./unitsquare.tex:26: LaTeX Error: Command \Box not provided in base LaTeX2e.

Question: How can I get that symbol with xelatex with the fonts in use for that?

\documentclass{article}

\usepackage[T2A,T1]{fontenc}
\usepackage{amsmath}    % avoid \dddot clash
\usepackage{mathtools}  % avoid unicode-math clash
\usepackage{amsthm} % avoid openbox clash

\usepackage{iftex}
\ifTUTeX
  \usepackage{fontspec}
  \usepackage{unicode-math}
  \setmainfont{TeX Gyre Termes}[Scale=1.0]
  \defaultfontfeatures{Scale=MatchLowercase, Ligatures=TeX}
  \setmathfont{TeX Gyre Termes Math}
\else
  \usepackage{newtxtext}  
  \usepackage{newtxmath}
\fi

\usepackage{scalerel}

\newcommand{\unitsq}{\mathord{\scalerel*{\Box}{X}}}

\begin{document}

The unit square is denoted by $\unitsq$.

\end{document}

Related: How to type in these "circle" and "square"?

3
  • Don't use fontenc with XeLaTeX, unless for very special situations. Preparing a document that works in both pdflatex and xelatex with no change is, unfortunately, just a dream.
    – egreg
    Oct 16, 2020 at 20:56
  • The \Box command is provided by the latexsym package, or \square in the newtxmath package
    – Guido
    Oct 16, 2020 at 21:05
  • @Guido: But for xelatex, I'm not using newtxmath. I don't know how to use latexsym with TeX Gyre Termes Math under xelatex.
    – murray
    Oct 16, 2020 at 21:19

2 Answers 2

3

You might have the impression that you can prepare a document that compiles with both pdflatex and xelatex. This is not true, I'm afraid.

Anyway, the symbol you're probably looking for is \lgwhtsquare corresponding to U+2B1C WHITE LARGE SQUARE, that's unfortunately unavailable with TeX Gyre Termes Math or \mdlgwhtsquare, corresponding to U+25A1 WHITE SQUARE.

If you want to scale the symbol to the height of a capital letter, you can use graphicx (or scalerel, if you prefer).

\documentclass{article}

\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{graphicx}

\usepackage{fontspec}
\usepackage{unicode-math}
\setmainfont{TeX Gyre Termes}
\setmathfont{TeX Gyre Termes Math}

\newcommand{\unitsq}{\text{\resizebox{!}{\fontcharht\font`X}{$\mdlgwhtsquare$}}}

\begin{document}

The unit square is denoted by $\unitsq$.

\end{document}

enter image description here

An attempt to get a “universal” document:

\documentclass{article}
\usepackage{iftex}

\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{graphicx}

\iftutex
  \usepackage{fontspec}
  \usepackage{unicode-math}
  \setmainfont{TeX Gyre Termes}
  \setmathfont{TeX Gyre Termes Math}
\else
  \usepackage[T1]{fontenc}
  \usepackage{newtxtext,newtxmath}
\fi

\iftutex
  \newcommand{\unitsq}{\text{\resizebox{!}{\fontcharht\font`X}{$\mdlgwhtsquare$}}}
\else
  \newcommand{\unitsq}{\text{\resizebox{!}{\fontcharht\font`X}{$\Box$}}}
\fi

\begin{document}

The unit square is denoted by $\unitsq$.

\end{document}

With pdflatex I get

enter image description here

Why does LaTeX balk to \Box? Because you need amssymb for it unless a font package such as newtxmath provides by itself the AMS symbols. And no, unicode-math doesn't provide \Box. Maybe it should…

1
  • Perfect! I forgot about \mdlgwhtsquare, which I've used for other purposes with xelatex.
    – murray
    Oct 16, 2020 at 21:32
1

If you would rather have a larger square than TeX Gyre Termes Math has, you can borrow one from another math font.

\documentclass{article}
\usepackage{unicode-math}

\setmainfont{TeX Gyre Termes}
\setmathfont{TeX Gyre Termes Math}
\setmathfont{STIX Two Math}[
  range=\lgwhtsquare,
  Scale=MatchUppercase ]
\newcommand\unitsq{\lgwhtsquare}

\begin{document}

The unit square is denoted by $\unitsq$.

\end{document}

TeX Gyre Termes + STIX Two sample

There are numerous white squares in unicode-math, including \lgwhtsquare, \mdlgwhtsquare, \mdwhtsquare, \mdsmwhtsquare and \smwhtsquare. If none of these are the exact size you want, you can even load any of them with a tweaked Scale= option.

2
  • None of these from STIX Two Math seems to both fit with its bottom on the baseline and, when scaled to the size of an uppercase letter, retains sufficient thickness. I'm trying to match, in size and position, a unit circle created with tikz by \newcommand\TikCircle[1][1.125]{\tikz[baseline=-3*#1]{\draw[thick](0,0)circle[radius=#1mm];}} \newcommand{\unitcirc}{\TikCircle}.
    – murray
    Oct 18, 2020 at 15:14
  • @murray Okay, that was just an example. However: the same font has \lgwhtcircle, \mdlgwhtcircle, \mdwhtcircle, \mdsmwhtcircle, \smwhtcircle and \vysmwhtcircle, all of which should match if you give them the same scaling. One of those might work for you?
    – Davislor
    Oct 18, 2020 at 15:34

You must log in to answer this question.

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