2

Currently I would like to define whitespace that has this exact whitespace format

{ a, b, c }
{}
f : X -> Y
X:Y

(Notice the lack of space in {} and X:Y).

I would also like to style some of the greek characters with my own style, as well as the color of the punctuation characters.

Currently what I am doing is this:

\mylcurly a \mycomma b \mycomma c \myrcurly

But (besides being verbose) it breaks down in the case of { a, b, c } and {} because the whitespace is different for the two curly bracket pairs, so I end up having to do this:

\mylcurlya a \mycomma b \mycomma c \myrcurlya
\mylcurlyb \myrcurlyb

Same goes for the colon : in the initial example.

Essentially what I would like to do is twofold:

  1. Manage whitespace correctly.
  2. Manage custom colored characters correctly.

(It seems the LaTeX engine does all kinds of interesting stuff to the text behind the scenes, so wondering if I can somehow do the same).

I am wondering if my approach is the right one, or if there is a more LaTeX-y way of handling it. Perhaps there is a way to specify a style guide for the text in general, such as using pattern matching to define what sorts of things to style a certain way. For example, maybe there is a way to do this:

p > {
  becomes
\mylcurly

A MWE is this:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{color}

\newcommand{\myin}{\;\textcolor{blue}{\in}\;}
\newcommand{\mycolon}{\;\;\textcolor{blue}{:}\;\;}
\newcommand{\mycolonb}{\textcolor{blue}{:}}
\newcommand{\myrarrow}{\;\;\textcolor{blue}{\rightarrow}\;\;}
\newcommand{\mylarrow}{\;\;\textcolor{blue}{\leftarrow}\;\;}
\newcommand{\myeq}{\;\textcolor{blue}{=}\;}
\newcommand{\myand}{\;\textcolor{blue}{\land}\;}
\newcommand{\myor}{\;\textcolor{blue}{\lor}\;}
\newcommand{\mylcurly}{\;\textcolor{blue}{\{}\;}
\newcommand{\myrcurly}{\;\textcolor{blue}{\}}\;}
\newcommand{\mycomma}{\textcolor{blue}{,}\;}

\begin{document}

$\mylcurly a \mycomma b \mycomma c \myrcurly \mycomma \mylcurly \dots$

$\mylcurly\myrcurly$

$f \mycolon X \myrarrow Y$

$X \mycolon Y$

\end{document}

It shows the output like this:

{ a, b, c }, { ...
{ }
f : X -> Y
X : Y

when the output should be:

{ a, b, c }, { ...
{}
f : X -> Y
X:Y

I understand how to make it so the whitespace fits that pattern in this example. What I am not sure of is if this is the generally correct approach in "styling the text", or if there is a better way to do it that handles the edge cases of things like the lack of space in {} and same with X:Y in a better way (less ad hoc). In practice there are more examples of whitespace being off in different contexts, these are just a few simple ones I included in the MWE to simplify the question.

Update

Here is the original and the desired image if that's helpful. Also here is the source code that made the desired image.

$\mylcurly a \mycomma b \mycomma c \myrcurly\!\! \mycomma \mylcurly \dots$

$\mylcurly\!\!\!\myrcurly$

$f \mycolon X \myrarrow Y$

$X\!\!\! \mycolon\!\!\! Y$

enter image description here enter image description here

Lance
  • 1,799
  • 1
    you are losing all the math class spacing: rather than do \newcommand{\myin}{\;\textcolor{blue}{\in}\;} it would be much better to do \newcommand{\myin}{\mathrel{\textcolor{blue}{\in}}} and let tex autimatically space this as a mathrel (like \in) rather than having fixed ; spacing. – David Carlisle Apr 21 '18 at 14:46
  • I would like to customize the general math spacing in the end, wondering how to do that. – Lance Apr 21 '18 at 14:48
  • 1
    sometimes it ends up being necessary to add fixed spaces such as \; but it should be avoided as far as possible as such spaces don't vanish automatically if for example the symbol is used at the start or end of a list. you can change the math class of symbols and you can change the size of the \medmuskip and \thickmuskip spaces that are added. But it is hard to suggest explicit settings in this generality. – David Carlisle Apr 21 '18 at 14:52
  • You don't like Donald Knuth's typography? What are you suggesting (in detail) instead? – Peter Wilson Apr 21 '18 at 18:08
  • @PeterWilson The details are the spaces I want around the elements. I want to control the spacing more fine-grained. I don't know how other than what I am already doing in the example. – Lance Apr 21 '18 at 18:23
  • it would help if you could show the exact output you would like to see. that involves forcibly inputting all desired spaces in a tiny example file, converting it to .png, and adding it to your question. here's a suggested technique that has worked for me: https://tex.stackexchange.com/a/247690/579 – barbara beeton Apr 22 '18 at 18:18
  • @barbarabeeton ok I have updated with a picture example. Thank you. – Lance Apr 22 '18 at 18:32
  • 1
    thanks. that makes a lot more sense. i'm unable to test anything right now (sending this from a laptop without a tex system installed, and the main system i work from suffered a power failure earlier today and is still out of commission). but what you want can likely be accomplished by assigning the proper class to each symbol, e.g., \mathord, \mathbin, \mathrel, etc. – barbara beeton Apr 23 '18 at 00:06
  • If the big problem is the {}, have you considered defining \myemptybraces? – Davislor Apr 23 '18 at 06:08
  • Also, I have no idea why you expect \mycolon to behave differently the two times you use it. I notice that there is an unused \mycolonb, however. Was one of them supposed to be that? – Davislor Apr 23 '18 at 06:11
  • You can get something very close to the { a, b, c } and {} you want without adding spaces manually by writing the first expression as { \mathop{a,} \mathop{b,} \mathop{c,} }. – Davislor Apr 23 '18 at 06:34
  • I’m not sure what you want for the colons, since you want the same macro to be expanded two different ways. However, the default math class with the most spacing around it is \mathrel, followed by \mathbin, and if you want very tight spacing, you could go to \mathord. Note that you can change the spacing between two different math classes in LuaTeX. – Davislor Apr 23 '18 at 06:36

1 Answers1

2

Summarizing the comments, it sounds like there is no way to do this generically in LaTeX. Instead the suggested alternatives are:

  1. To use \mathrel, \mathbin, \mathop, or \mathord (What is the difference between \mathbin vs. \mathrel? and Difference between \mathop and \mathord)
  2. Change the spacing around operators or relations with \thickmuskip etc. (How to change default for spacing around binary relations?)
  3. Manually change the spacing (https://www.sharelatex.com/learn/Spacing_in_math_mode)

There is no way to use sort of CSS features and control the letter spacing in certain contexts otherwise, that is all implemented behind the scenes in LaTeX.

In my case I am not necessarily (semantically) changing an operator vs. a relation, so it is a hack to do it that way. I am just changing the spacing visually between arbitrary combinations of characters, sort of like ligatures. In that case it seems the best workaround is (3), manually spacing and dealing with the fact that you lose all of the math spacing features, since they end up just getting in the way anyways if you try and customize them too much.

Lance
  • 1,799