# Space on one side of a certain character is larger

I'm using pandoc with the XeLaTeX engine. I have a problem with the space around certain characters being larger on one side than on the other. Given a markdown like this:

X ≼   Y  // 3 spaces on the right

X ≼  Y  // 2 spaces on the right

X ≼ Y  // 1 space on the right


The generated latex (through pandoc) is:

\texttt{X\ ≼\ \ \ Y}

\texttt{X\ ≼\ \ Y}

\texttt{X\ ≼\ Y}


And the result is:

Notice that one space on the left-hand side of ≼ is equal to approximately 3 spaces on the right-hand side. Moreover, the Y character is printed using the wrong font when used after ≼.

What could be the problem? Is it because I'm using a fallback font just for this single character and it influences the spacing?

\usepackage{ucharclasses}
\newfontfamily{\fallbackfont}{FreeMono}
\newfontfamily{\mymainfont}{MyFont}
\setTransitionsForMathematics{\fallbackfont}{\mymainfont}


# Edit including whole markdown and generated LaTeX:

My markdown:

# MWE

Test: X ≼ Y test test

X ≼   Y

X ≼  Y

X ≼ Y

X ≼ Y


metadata.yml:

---
title: Title title
author: Michał Miszczyszyn
lang: pl-PL
toc: yes
documentclass: scrbook
papersize: b5
geometry: b5paper
mainfont: Merriweather
monofont: Fira Code
indent: true
thanks: Thanks
---


header.latex:

\addtokomafont{disposition}{\rmfamily}

\usepackage{indentfirst}
\setlength\parindent{24pt}
\setlength{\parskip}{.2\baselineskip}

\usepackage{setspace}
\onehalfspacing

\makeatletter
\def\verbatim@nolig@list{}
\makeatother

\usepackage{ucharclasses}
\newfontfamily{\fallbackfont}{FreeMono}
\newfontfamily{\mymainfont}{Merriweather}
\setTransitionsForMathematics{\fallbackfont}{\mymainfont}

\usepackage[all]{nowidow}


Pandoc build script:

pandoc \
-F pandoc-citeproc \
--pdf-engine=xelatex \
--standalone \
--toc \
--highlight-style=kate \
chapters/test.md \
-o out/out.latex


Pandoc-generated LaTeX:

% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
polish,
b5paper,
]{scrbook}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\setmainfont[]{Merriweather}
\setmonofont[]{Fira Code}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
pdftitle={Title title},
pdfauthor={Michał Miszczyszyn},
pdflang={pl-PL},
pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[b5paper]{geometry}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering

\usepackage{indentfirst}
\setlength\parindent{24pt}
\setlength{\parskip}{.2\baselineskip}

\usepackage{setspace}
\onehalfspacing

\makeatletter
\def\verbatim@nolig@list{}
\makeatother

\usepackage{ucharclasses}
\newfontfamily{\fallbackfont}{FreeMono}
\newfontfamily{\mymainfont}{Merriweather}
\setTransitionsForMathematics{\fallbackfont}{\mymainfont}

\usepackage[all]{nowidow}

\ifxetex
% Load polyglossia as late as possible: uses bidi with RTL langages (e.g. Hebrew, Arabic)
\usepackage{polyglossia}
\setmainlanguage[]{polish}
\else
\usepackage[shorthands=off,main=polish]{babel}
\fi

\title{Title title\thanks{Thanks}}
\author{Michał Miszczyszyn}
\date{}

\begin{document}
\frontmatter
\maketitle

{
\setcounter{tocdepth}{2}
\tableofcontents
}
\mainmatter
\hypertarget{mwe}{%
\chapter{MWE}\label{mwe}}

Test: \texttt{X\ ≼\ Y} test test

\texttt{X\ ≼\ \ \ Y}

\texttt{X\ ≼\ \ Y}

\texttt{X\ ≼\ Y}

X ≼ Y

\backmatter
\end{document}

• can you show the generated tex? If this is math mode then you would not expect spaces to make any difference at all. – David Carlisle Feb 2 '20 at 11:42
• The text you show us includes backticks, so is this supposed to be verbatim material? – Skillmon Feb 2 '20 at 11:47
• The generated LaTeX shows \texttt, which is essentially verbatim, not math, so spaces are not compressed. But even if it were coded as math, the presence of \ \ \  would still force the extra spaces. Only "typed" spaces are compressed and treated as single spaces. {I'm not a pandoc user, so can't say how this should be input to be treated properly as math.) – barbara beeton Feb 2 '20 at 20:37
• Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – DG' Feb 2 '20 at 20:57
• The problem seem to be, that your setup switches to Merriweather after ≼ even if the current font is FreeMono. That's why the spaces are messed up. If you only need a few symbols, you can set the fallback manually. – DG' Feb 2 '20 at 22:03

Apparently the switching back from the fallback is messed up in you example, i.e. it switches to \mymainfont after ≼ even within \texttt. That's why you end up with spaces of different lengths.

If you only want a very small number of fallback chars, I suggest setting them up manually:

\documentclass{scrbook}
\usepackage{unicode-math}
\setmainfont{Merriweather}
\setmonofont{FreeMono}
\usepackage{newunicodechar}
\newfontfamily{\fallbackfont}{FreeMono}
\DeclareTextFontCommand\textfallback{\fallbackfont}
\newunicodechar{≼}{\textfallback{≼}}

\begin{document}
Test: \texttt{X\ ≼\ Y} test test

\texttt{X\ ≼\ \ \ Y}

\texttt{X\ ≼\ \ Y}

\texttt{X\ ≼\ Y}

X ≼ Y
\end{document}