LaTeX forum ⇒ Math & ScienceSmart(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

Postby wmusial » Wed Sep 04, 2013 6:59 am

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.
  1. 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.
  1. i \hbar \partial_t \psi

Whereas ideally you'd like to adjust the spacing between the symbols to indicate their meaning and improve readability.
  1. 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:

User avatar
cgnieder
Site Moderator
Posts: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Wed Sep 04, 2013 2:51 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

Postby wmusial » Thu Sep 05, 2013 1:02 am

Thanks for the reply.

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 --> ab
c^{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!

User avatar
Johannes_B
Site Moderator
Posts: 4100
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Thu Sep 05, 2013 10:03 am

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.

  1. \documentclass{article}
  2. \usepackage{mathtools}
  3. \usepackage{chemmacros}
  4. \begin{document}
  5.  
  6. A chemical equation should look like this:
  7. \begin{reaction}
  8. 2 H2 + O2 -> 2 H2O
  9. \end{reaction}
  10. 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:
  11. \[
  12. ab c^{d} \hat e \mathbf{f}\]
  13. should be atomized as:
  14. \[ab, c^{d}, \hat e, \mathbf{f}\]
  15.  
  16. Atoms should be identified by the string being decorated (the "base"):
  17. \begin{gather}
  18. ab --> ab \\
  19. c^{d} --> c\\
  20. \hat e --> e \\
  21. \mathbf{f} --> f
  22. \end{gather}
  23.  
  24.  
  25. \iffalse
  26. Atoms belong to categories depending on their base.
  27.  
  28. Adjacent atoms in an expression should be grouped by the category they belong to. Example:
  29. Category 1 -- i, \hbar
  30. Category 2 -- \partial
  31. Category 3 -- \psi
  32. \fi
  33.  
  34.  
  35. equation:
  36. \[\psi i \hbar \partial_t \psi\]
  37. ... should be grouped as (parentheses below and in all what follows denote the logical grouping and are not printed):
  38. \[(\psi) (i \hbar) (\partial_t) (\psi)\]
  39.  
  40. \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

Postby wmusial » Fri Sep 06, 2013 8:29 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:
  1. \documentclass{article}
  2.  
  3. \setlength{\parindent}{0pt}
  4. \usepackage{mathtools}
  5. \usepackage[margin=1in]{geometry}
  6. \newcommand{\noparskip}{}
  7.  
  8.  
  9. \begin{document}
  10. \subsubsection*{setup}
  11.  
  12. \begin{itemize}
  13. \item
  14. Let'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:
  15. \noparskip
  16. \begin{verbatim}
  17. ab c^{d} \hat e \mathbf{f}
  18. \end{verbatim}
  19. \noparskip
  20. which compiles to:
  21. \[ab c^{d} \hat e \mathbf{f}\]
  22. should be atomized as:
  23. \noparskip
  24. \begin{verbatim}
  25. ab, c^{d}, \hat e, \mathbf{f}
  26. \end{verbatim}
  27.  
  28. \item
  29. Atoms should be identified by the string being decorated (the "base"):
  30. \noparskip
  31. \begin{verbatim}
  32. \texttt{ab} --> ab
  33. c^{d} --> c
  34. \hat e --> e
  35. \mathbf{f} --> f
  36. \end{verbatim}
  37.  
  38. \item
  39. Atoms belong to categories depending on their base.
  40.  
  41. \item
  42. Adjacent atoms in an expression should be grouped by the category they belong to.
  43.  
  44. Consider the following equation:
  45. \begin{verbatim}
  46. \psi i \hbar \partial_t \psi
  47. \end{verbatim}
  48. which typesets to:
  49. \[
  50. \psi i \hbar \partial_t \psi
  51. \]
  52.  
  53. The atom categories are:
  54.  
  55. \begin{itemize}
  56. \item Category 1 -- \texttt{i}, \texttt{\textbackslash hbar}
  57. \item Category 2 -- \texttt{\textbackslash partial}
  58. \item Category 3 -- \texttt{\textbackslash psi}
  59. \end{itemize}
  60.  
  61. and the resulting (logical) grouping is: (parentheses below and in all what follows denote the logical grouping and are not printed)
  62.  
  63. \begin{verbatim}
  64. (\psi) (i \hbar) (\partial_t) (\psi)
  65. \end{verbatim}
  66.  
  67. \end{itemize}
  68.  
  69. \subsubsection*{rules}
  70.  
  71. \begin{itemize}
  72. \item
  73. Adjacent tokens within one group should be separated by \texttt{\textbackslash,}.
  74. Example (parentheses denote logical grouping and are not printable):
  75. \begin{verbatim}
  76. (\psi) (i \, \hbar) (\partial_t) (\psi)
  77. \end{verbatim}
  78. which typesets to:
  79. \[
  80. \psi i \, \hbar \partial_t \psi
  81. \]
  82. \item
  83. Separation between groups is more complicated.
  84. 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.
  85. 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.
  86. The above equation results in:
  87. \begin{verbatim}
  88. (\psi) \quad (i \, \hbar) \; (\partial_t) (\psi)
  89. \end{verbatim}
  90. which typesets to:
  91. \[
  92. \psi \quad i \, \hbar \; \partial_t \psi
  93. \]
  94.  
  95. \item
  96. In addition, printable non-alphanumerical characters should break the grouping and should not be surrounded by any spacing separators.
  97. Example:
  98. \begin{verbatim}
  99. i \hbar \cdot i \hbar \psi
  100. \end{verbatim}
  101. becomes:
  102. \begin{verbatim}
  103. (i \, \hbar) \cdot (i \, \hbar) \; (\psi)
  104. \end{verbatim}
  105. \end{itemize}
  106.  
  107. \end{document}


Return to “Math & Science”

Who is online

Users browsing this forum: No registered users and 2 guests