Title: Whitespace in mathtext is too large and inconsistent with LaTeX · Issue #4335 · matplotlib/matplotlib · GitHub
Open Graph Title: Whitespace in mathtext is too large and inconsistent with LaTeX · Issue #4335 · matplotlib/matplotlib
X Title: Whitespace in mathtext is too large and inconsistent with LaTeX · Issue #4335 · matplotlib/matplotlib
Description: Hi matplotlib developers, As a user quite familiar with LaTeX, I am constantly frustrated with the display size of the whitespace commands in matplotlib's mathtext mode with 'text.usetex':False. The matplotlib mathtext spacing commands a...
Open Graph Description: Hi matplotlib developers, As a user quite familiar with LaTeX, I am constantly frustrated with the display size of the whitespace commands in matplotlib's mathtext mode with 'text.usetex':False. Th...
X Description: Hi matplotlib developers, As a user quite familiar with LaTeX, I am constantly frustrated with the display size of the whitespace commands in matplotlib's mathtext mode with 'text.usetex...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/4335
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Whitespace in mathtext is too large and inconsistent with LaTeX","articleBody":"Hi matplotlib developers,\n\nAs a user quite familiar with LaTeX, I am constantly frustrated with the display size of the whitespace commands in matplotlib's mathtext mode with `'text.usetex':False`. The matplotlib mathtext spacing commands are much larger than their LaTeX equivalents. Here is a minimal working example:\n\n``` python\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\nmpl.rcParams.update({\n 'text.usetex':False,\n 'font.family':'sans-serif',\n 'font.sans-serif':['DejaVu Sans'], # Needs a true unicode font\n 'mathtext.default':'regular',\n })\n\ndef M(space):\n return \"${0}$ '{1}'\\n\".format(space.join(8*['M']),space)\n\ntext = (\n M(r'\\! ')+\n M(r'')+\n M(r'\\hspace{0.1} ')+\n M(r'\\, ')+\n M(r'\\thinspace ')+\n M(r'\\/ ')+\n M(r'\\; ')+\n M(r'\\ ')+\n M(r'\\enspace ')+\n M(r'\\quad ')+ \n M(r'\\qquad ')\n )\n\nplt.figure(figsize=(8,3))\nplt.axis('off')\nplt.text(0,1,text,va='top')\nplt.show()\n```\n\n\n\nTested with matplotlib 1.4.3, python 2.7 on Windows 7 and Linux.\nThe `\\thinspace` command and the `\\enspace` command are handled as Unicode characters, so this requires a true Unicode font, such as \"DejaVu Sans\", not the builtin default \"Bitstream Vera Sans\".\n\nIn the example shown above, the spacing between the series of \"M\" characters is much much bigger than expected for the commands `\\,` and `\\;` and `\\quad` and `\\qquad`. Also, the spacing should be monotonically increasing from top to bottom, but it clearly does not.\nFor instance, in LaTeX, the `\\,` command is equivalent to `\\thinspace`, and should be 0.16667em wide, but in matplotlib, `\\,` is not thin at all, and is much larger than `\\thinspace`. This means that `\\,` is useless when one wants to create a truly-thin space in math mode.\nSimilarly, `\\;` should be much smaller than `\\enspace`.\n\n@mdboom After poking around in the source code, I discovered that the default values of these mathtext lengths are defined in \"mathtext.py\" by this dictionary:\n\n``` python\n _space_widths = { r'\\ ' : 0.3,\n r'\\,' : 0.4,\n r'\\;' : 0.8,\n r'\\quad' : 1.6,\n r'\\qquad' : 3.2,\n r'\\!' : -0.4,\n r'\\/' : 0.4 }\n```\n\nwhere the dictionary values are the fractional width of the font (in \"em\" units).\nI don't know where these numbers came from, but they are very different than the LaTeX specification, which can be found here:\nhttp://tex.stackexchange.com/a/74354\nor if you can find a copy of the \"TeXbook\" by Donald Knuth himself, or there are probably many other source on the web.\n\nI propose to change this dictionary to the same values used by LaTeX:\n\n``` python\n _space_widths = { r'\\,' : 0.16667, # 3/18 em = 3 mu\n r'\\thinspace' : 0.16667, # 3/18 em = 3 mu\n r'\\/' : 0.16667, # 3/18 em = 3 mu\n r'\\\u003e' : 0.22222, # 4/18 em = 4 mu\n r'\\:' : 0.22222, # 4/18 em = 4 mu\n r'\\;' : 0.27778, # 5/18 em = 5 mu\n r'\\ ' : 0.33333, # 6/18 em = 6 mu\n r'\\enspace' : 0.5, # 9/18 em = 9 mu\n r'\\quad' : 1, # 1 em = 18 mu\n r'\\qquad' : 2, # 2 em = 36 mu\n r'\\!' : -0.16667, # -3/18 em = -3 mu\n } \n```\n\nAnd while, we are at it, we can add the LaTeX commands for `\\\u003e` and `\\:`, and we can remove the Unicode dependency for `\\thinspace` and `\\enspace` by adding definitions to this dictionary.\n(I don't think `\\/` is normally used by LaTeX in math mode, but in earlier versions of matplotlib it was defined to be equal to `0.1`em, so it was originally intended to be a thin space.)\nAfter making this change, the spacing behaves as expected:\n\n\n\nHowever, I realize that this is a backwards incompatible change that will affect users out in the wild, but I think that it is worth the change to more sensible defaults, as long as it is well documented.\nI imagine that lots of people, besides myself, expect matplotlib's mathtext to behave like LaTeX.\n\nToday I discovered the undocumented `\\hspace{float}` command in matplotlib's mathtext, that is similar to the LaTeX command by the same name (except that lengths in LaTeX have units). This provides a temporary workaround for me, but is annoying to have to use for such a common task as creating a thin space in math mode.\n\nThanks for the consideration.\n","author":{"url":"https://github.com/u55","@type":"Person","name":"u55"},"datePublished":"2015-04-14T07:26:54.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/4335/matplotlib/issues/4335"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:003c3b9a-2a30-8f94-b807-5f09ded1d53a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B6E0:334ED5:8DA0DF:BE18D8:6A528FF6 |
| html-safe-nonce | 12f8937aea9d77a40950ae6a2d510d03b7cc5c14da120d99e164ad8d8812a3f2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNkUwOjMzNEVENTo4REEwREY6QkUxOEQ4OjZBNTI4RkY2IiwidmlzaXRvcl9pZCI6Ijg1Njk5OTU4OTk2MDE4NTAzNTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b182ad904bc7c22552c1fd25ec984630da25be739c170cbc7ff9ae06f2c76245 |
| hovercard-subject-tag | issue:68290732 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/4335/issue_layout |
| twitter:image | https://opengraph.githubassets.com/1cd50cc2ee028e6ed55027fc3520788f8434ff7a3f532c8cacfadc423e5e0241/matplotlib/matplotlib/issues/4335 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/1cd50cc2ee028e6ed55027fc3520788f8434ff7a3f532c8cacfadc423e5e0241/matplotlib/matplotlib/issues/4335 |
| og:image:alt | Hi matplotlib developers, As a user quite familiar with LaTeX, I am constantly frustrated with the display size of the whitespace commands in matplotlib's mathtext mode with 'text.usetex':False. Th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | u55 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width