## LaTeX forum ⇒ Math & Science ⇒ Smart(er) Spacing in Math Mode

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
wmusial
Posts: 3
Joined: Wed Sep 04, 2013 6:47 am

### Smart(er) Spacing in Math Mode

hello,

I use LaTeX for taking quick (physics) notes that involve tons of equations and I'd be interested in developing some LaTeX code to automatically beautify equations in order to increase readability. A lot of times in physics write up there show up equations of the following form.
i \hbar \partial_t \psi

That involve both constants (i, \hbar), differential operators (\partial_t) and physically meaningful quantities (\psi). when you just string them together like in the example above, everything gets mashed up together.
i \hbar \partial_t \psi

Whereas ideally you'd like to adjust the spacing between the symbols to indicate their meaning and improve readability.
i \, \hbar \; \partial_t \psi

Inserting of \,, \; and \quad gets clumsy and annoying real quick when scribing live. At the same time, the rules for inserting the spacing are pretty algorithmic (at least within a given field).

I'd like to develop a set of rules that'd adjust the spacing at typeset time, but I have never open Pandora's box that LaTeX is and have no clue where to start. I could probably put together a quick and dirty reg-ex pre-processing script to insert the spacing symbols, but that's, well, dirty. I'd appreciate any pointers, and I can probably take it from there.

Tags:

cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm
Hi wmusial,

Welcome to the LaTeX community. We can certainly help with the TeXnical side. What we would need, though, is to know the set of rules you mention. Something like: »every time i is followed by \hbar a \, should be inserted«. You are certainly more qualified to come up with these rules as you've actually been typing these things.

Once the rules are clear we should be able to tell if and how a solution for them could be done.

Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

wmusial
Posts: 3
Joined: Wed Sep 04, 2013 6:47 am

Consider the following setup. Let's take atom to mean a sequence of printable alphanumerical characters together with all its possible decorations (super-/sub-scripts, \vec, \hat, etc). Atoms are separated by one or more spaces. For example, the following equation:
ab c^{d} \hat e \mathbf{f}
should be atomized as:
ab, c^{d}, \hat e, \mathbf{f}

Atoms should be identified by the string being decorated (the "base"):
ab --> abc^{d} --> c\hat e --> e\mathbf{f} --> f

Atoms belong to categories depending on their base.

Adjacent atoms in an expression should be grouped by the category they belong to. Example:
Category 1 -- i, \hbar
Category 2 -- \partial
Category 3 -- \psi

equation:
\psi i \hbar \partial_t \psi
... should be grouped as (parentheses below and in all what follows denote the logical grouping and are not printed):
(\psi) (i \hbar) (\partial_t) (\psi)

As for the rules, let's start with the following. Adjacent tokens within one group should be separated by \,. Example:
(\psi) (i \, \hbar) (\partial_t) (\psi)

Separation between groups is more complicated. The default behaviour is to separate adjacent groups by \;, but groups can override this behaviour by specifying a pre- and a postfix separator. For example, let's say Category 1 should be preceded by \quad instead of \,, and Category 3 should be followed by nothing. The above equation results in:
(\psi) \quad (i \, \hbar) \; (\partial_t) (\psi)

In addition, printable non-alphanumerical characters should break the grouping and should not be surrounded by any spacing separators. Example:
i \hbar \cdot i \hbar \psi
becomes:
(i \, \hbar) \cdot (i \, \hbar) \; (\psi)

I hope this makes the rules clear. Since i have no idea how LaTeX organizes things internally, nor what sort of hacks are possible, I expect that some of the ideas cannot be implemented in their current form, or possibly not at all. I'm open to suggestions!

Johannes_B
Site Moderator
Posts: 4077
Joined: Thu Nov 01, 2012 4:08 pm
Would it be possible to show what you are trying to achieve directly with code? Something like the following would be really great.
You can click on Open in writelatex just above the code and directly see the output. You have to mark up the code using the code-button.

\documentclass{article}\usepackage{mathtools}\usepackage{chemmacros}\begin{document} A chemical equation should look like this:\begin{reaction}2 H2 + O2 -> 2 H2O\end{reaction}Consider the following setup. Let's take atom to mean a sequence of printable alphanumerical characters together with all its possible decorations (super-/sub-scripts, \verb+\vec, \hat+, etc). Atoms are separated by one or more spaces. For example, the following equation:$ab c^{d} \hat e \mathbf{f}$should be atomized as:$ab, c^{d}, \hat e, \mathbf{f}$ Atoms should be identified by the string being decorated (the "base"):\begin{gather}ab --> ab \\c^{d} --> c\\\hat e --> e \\\mathbf{f} --> f\end{gather}  \iffalseAtoms belong to categories depending on their base. Adjacent atoms in an expression should be grouped by the category they belong to. Example:Category 1 -- i, \hbarCategory 2 -- \partialCategory 3 -- \psi\fi  equation:$\psi i \hbar \partial_t \psi$... should be grouped as (parentheses below and in all what follows denote the logical grouping and are not printed):$(\psi) (i \hbar) (\partial_t) (\psi)$ \end{document}

In the meantime, you should take a look at section 11 of mathmode.

You are talking about atoms. Could you give some real-life examples?
Somehow i can't get rid of the feeling, that you are trying to typeset chemical formulas as well. If so, you should have a look at chemmacros. The physics-package might also be helpful for you.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

wmusial
Posts: 3
Joined: Wed Sep 04, 2013 6:47 am
Hello,

I was not in fact thinking of chemical equations. I'm not sure if that's the source of feeling, but just to clarify by atom I mean a basic unit, or a token, not atom as in chemstry.

The physics package provides a whole bunch of very useful shorthands, but it does not deal with the issue of spacing.

the chemmacros looks like it could be doing some relevant things internally, i'll try to read the source.

The mathmode docs do shine some light in how the spacing is implemented. It seems to me that LaTeX atomizes the equation and follows a set of rules to insert one of the three lengths (thinmuskip, medmuskip, thickmuskip) in between the atoms. What i'm after is modifying of those rules in such a way that the spacing is chosen in a way I described in the previous post.

I didn't know about the typesetting of the whole document. For what it is worth, here's the previous post in latex:
\documentclass{article} \setlength{\parindent}{0pt}\usepackage{mathtools}\usepackage[margin=1in]{geometry}\newcommand{\noparskip}{}  \begin{document}\subsubsection*{setup} \begin{itemize}\itemLet's take \emph{atom} to mean a sequence of printable alphanumerical characters together with all its possible decorations (super-/sub-scripts, \texttt{\textbackslash vec}, \texttt{\textbackslash hat}, etc). Atoms are separated by one or more spaces. For example, the following latex equation code:\noparskip\begin{verbatim}ab c^{d} \hat e \mathbf{f}\end{verbatim}\noparskipwhich compiles to:$ab c^{d} \hat e \mathbf{f}$should be atomized as:\noparskip\begin{verbatim}ab,  c^{d},  \hat e,  \mathbf{f}\end{verbatim} \itemAtoms should be identified by the string being decorated (the "base"):\noparskip\begin{verbatim}\texttt{ab} --> abc^{d} --> c\hat e --> e\mathbf{f} --> f\end{verbatim} \itemAtoms belong to categories depending on their base. \itemAdjacent atoms in an expression should be grouped by the category they belong to. Consider the following equation:\begin{verbatim}\psi i \hbar \partial_t \psi\end{verbatim}which typesets to:$\psi i \hbar \partial_t \psi$ The atom categories are: \begin{itemize}\item Category 1 -- \texttt{i}, \texttt{\textbackslash hbar}\item Category 2 -- \texttt{\textbackslash partial}\item Category 3 -- \texttt{\textbackslash psi}\end{itemize} and the resulting (logical) grouping is: (parentheses below and in all what follows denote the logical grouping and are not printed) \begin{verbatim}   (\psi)  (i \hbar)  (\partial_t)  (\psi)\end{verbatim} \end{itemize} \subsubsection*{rules} \begin{itemize}\itemAdjacent tokens within one group should be separated by \texttt{\textbackslash,}.Example (parentheses denote logical grouping and are not printable):\begin{verbatim}   (\psi)  (i \, \hbar)  (\partial_t)  (\psi)\end{verbatim}which typesets to:$\psi i \, \hbar \partial_t \psi$\item  Separation between groups is more complicated.The default behaviour would be to separate adjacent groups by \texttt{\textbackslash;} but groups can override this behaviour by specifying a pre- and a postfix separator.For the purpose of an example, let's say Category 1 should be preceded by \texttt{\textbackslash quad} instead of \texttt{\textbackslash,} and Category 3 should be followed by nothing.The above equation results in:\begin{verbatim}  (\psi)  \quad  (i \, \hbar) \; (\partial_t)  (\psi)\end{verbatim}which typesets to:$\psi \quad i \, \hbar \; \partial_t \psi$ \itemIn addition, printable non-alphanumerical characters should break the grouping and should not be surrounded by any spacing separators.Example:\begin{verbatim}   i \hbar \cdot i \hbar \psi\end{verbatim}becomes:\begin{verbatim}   (i \, \hbar) \cdot (i \, \hbar) \; (\psi)\end{verbatim}\end{itemize} \end{document}