1

I find LaTeX's automatic spacing for a lot of common mathematical expressions to be extremely ugly, especially with respect to brackets. For example:

$f(x+a)$ and $\mathbf{P}(x=a)$ both have "x" and "a" closer to the bracket than to the equal-sign.

To mitigate that, I would like to increase the spacing after ( and before ) automatically in all math. How can I do that? (I definitely do not want to manually insert \, all over to obtain a better spacing.) Unfortunately, I am not familiar enough with TeX to know how to meddle with primitives like brackets.

user21820
  • 868
  • 7
  • 19
  • tex is not expecting = to be used in a function argument so it gets wide spacing for a top level relation, rather than change the spacing for brackets (which is hard as tex inserts no space there, so no standard parameter that you can change) I would use a tighter = and use (x{=}a) – David Carlisle Oct 03 '20 at 14:26
  • @DavidCarlisle: I know there are all sorts of tricks for eliminating spaces or adding spaces manually, but it makes no sense to do that for every instance of the first example (which does not use "="). – user21820 Oct 03 '20 at 14:32
  • And yes I know that there is no simple parameter that can be changed, which is why I am asking this question. I know that it will require a low-level redefinition of brackets. – user21820 Oct 03 '20 at 14:33
  • 2
    well as I say tex has no parameter to control the space around ( and ) so you would need to modify the font metrics (not so easy) or make ( and ) active and act as macros to add the space (hard and very fragile if you do not want to break \left( ) so it would almost certainly break in real documents. But apart from that I think reducing the space around = is the more correct change than increasing the space around () – David Carlisle Oct 03 '20 at 14:34
  • @DavidCarlisle: Did you read my question and first comment carefully? I said the problem applies to all binary operations as well, not just the equal-sign. Are you saying I should decrease all the muskips to the minimum? That doesn't make sense. As I said, I do not want to do it manually. – user21820 Oct 03 '20 at 14:42
  • well actually you only commented on the = sign, although you gave a fragment using a + as well, but anyway; OK it's not as if I downvoted the question, the the question itself is reasonable, it's just that I expect that it has no reasonable answer using tex (using luatex you may have a better chance) defining ) to add space before in a way that does not break \right) would be hard (if I could think of a way at all) and would almost certainly not work in real documents, so the comments are just a warning that no answer coming from me, and I would be surprised if you get one at all. – David Carlisle Oct 03 '20 at 14:49
  • I don't see why it doesn't make sense to define a command such as \apply{f}{x+a} and \apply{\mathbf{P}}{x=a} then the \apply macrocan do\mathop{{}#1}\left(,#2,\right)` if that is what you want. – David Carlisle Oct 03 '20 at 14:54
  • @DavidCarlisle: If you have an explanation as to why what I'm asking for would be hard or impossible, it would be helpful, because I do not understand what is so difficult about it. I have played with hacking \left and \right before and have used my hacks for years with no trouble, so presumably it should be possible to hack ( and ) as well, but that is really too low-level for me. Regarding your last comment, it's too cumbersome. When my document is full of functions, I don't want to spend half my time typing macros, if there is a once-for-all solution (no matter how hacky). – user21820 Oct 03 '20 at 14:58
  • as I say I can not think of an answer that does what you want. So I can't really give a detailed breakdown of why it is hard:-) You are just asking for a feature that TeX doesn't have. – David Carlisle Oct 03 '20 at 15:06

2 Answers2

2

Despite the comments I would use a macro here and also as seen in this example I think that line three with extra space is worse than the default layout, and I prefer the final layouts with the tighter spacing in arguments. This is all relatively subjective of course, no right and wrong.

enter image description here

\documentclass{article}

\usepackage{amsmath} \begin{document}

\newcommand\apply[2]{#1(#2)} [\apply{f}{x+a} \qquad \apply{\mathbf{P}}{x=a}]

\renewcommand\apply[2]{#1\left(#2\right)} [\apply{f}{x+a} \qquad \apply{\mathbf{P}}{x=a}]

\renewcommand\apply[2]{#1\left(,#2,\right)} [\apply{f}{x+a} \qquad \apply{\mathbf{P}}{x=a}]

\renewcommand\apply[2]{#1\left(\hbox{$\thinmuskip=0mu\medmuskip=0mu\thickmuskip=0mu#2$}\right)} [\apply{f}{x+a} \qquad \apply{\mathbf{P}}{x=a}]

\renewcommand\apply[2]{#1\mathopen{}{\left(\hbox{$\thinmuskip=0mu\medmuskip=0mu\thickmuskip=0mu#2$}\right)}\mathclose{}} [\apply{f}{x+a} \qquad \apply{\mathbf{P}}{x=a}]

\end{document}

David Carlisle
  • 757,742
  • In my question I said extra spacing only after ( and before ), so it does not look like your line 3. =) [Edit: Oh I forgot that my hack of left and right already removes the excess space before \left(.] – user21820 Oct 03 '20 at 15:08
  • Anyway although line 4 looks nicer, it doesn't when the inner expression is a much longer one, such as \mathbf{P}( y = (f(x)+1)^2 ). – user21820 Oct 03 '20 at 15:11
  • @user21820 yes I should probably add a line fixing up the leftright spacing of the last version to restore the \mathopen spacing – David Carlisle Oct 03 '20 at 15:12
  • perhaps not but there are degees in between: I set all three to 0 which might be excessively squashed:-) @user21820 – David Carlisle Oct 03 '20 at 15:13
  • @user21820 If you think this answer and/or my comments under the question will dissuade others from answering in a way you would prefer ping me and I'll delete the comments and/or this answer. (although I think this is the answer:-) – David Carlisle Oct 03 '20 at 15:17
  • I have no problem with you posting this (because you think it is the best). =) However, I will of course not upvote or accept it because it didn't address my question. =) – user21820 Oct 03 '20 at 15:19
  • I just thought of something. Even if we cannot hack ( and ) to automatically add spacing, we can hack \left and \right to do that, right? Then at least we can have more spacing whenever we use parenthesis pairs. Do you have a way in that case? – user21820 Oct 12 '20 at 16:04
  • I found a way to solve this problem by hacking ( and ) and \left and \right, and it works as long as I don't use \boldsymbol. I am guessing that this is because \boldsymbol attempts to handle brackets next to the bolded symbol. Is there a way to solve this \boldsymbol problem? I guess that it is related to this and/or this. – user21820 Feb 19 '21 at 19:59
  • @user21820 \boldsymbol does nothing special about brackets. – David Carlisle Feb 19 '21 at 20:12
  • Since I have at last found what I feel is the correct answer to this question, do you mind deleting your answer? If you do, then never mind. =) – user21820 Feb 20 '21 at 14:01
  • @user21820 usual thing would be for you to simply accept your answer, no harm in leaving another answer for those not wanting to risk redefining \left and \right – David Carlisle Feb 20 '21 at 14:04
  • Yep I know that's the usual thing; I asked only because of your comment last october. – user21820 Feb 20 '21 at 14:14
  • yes I know but that was when there were no other answers and I offered to encourage them, but now there is another answer (and it's somewhat dangerous to suggest to people to redefine \left) I think I'll leave this one up as an alternative but I have no complaint if you accept your own:-) – David Carlisle Feb 20 '21 at 14:18
  • Ahh I see okay great! =) – user21820 Feb 20 '21 at 14:47
0

I finally found a solution to automatically increases spacing after ( and before ). For those modified by \left and right it is easy:

\renewcommand\left[1]{\mathopen{}\mathclose\bgroup\ifx#1(\originalleft(\,\else\originalleft#1\fi}
\renewcommand\right[1]{\aftergroup\egroup\ifx#1)\,\originalright)\else\originalright#1\fi}

For ordinary brackets, we need to use a trick to make them active. Applied here we get:

\begingroup\lccode`~=`(\lowercase{\endgroup\def~}{\oldlbrac\,}
\AtBeginDocument{\mathchardef\oldlbrac=\mathcode`(\mathcode`(="8000}
\begingroup\lccode`~=`)\lowercase{\endgroup\def~}{\,\oldrbrac}
\AtBeginDocument{\mathchardef\oldrbrac=\mathcode`)\mathcode`)="8000}
\makeatletter
\def\resetMathstrut@{%
    \setbox\z@\hbox{%
        \mathchardef\@tempa\mathcode`\[\relax
        \def\@tempb##1"##2##3{\the\textfont"##3\char"}%
        \expandafter\@tempb\meaning\@tempa \relax
    }%
    \ht\Mathstrutbox@\ht\z@ \dp\Mathstrutbox@\dp\z@}
\makeatother

Here is complete working example:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[T1]{fontenc}

\newlength\bracskip\setlength{\bracskip}{.04em plus .04em minus .02em} \newcommand\bracspace{\hspace{\bracskip}} \begingroup\lccode~=(\lowercase{\endgroup\def~}{\oldlbrac\bracspace} \AtBeginDocument{\mathchardef\oldlbrac=\mathcode(\mathcode(="8000} \begingroup\lccode~=)\lowercase{\endgroup\def~}{\bracspace\oldrbrac} \AtBeginDocument{\mathchardef\oldrbrac=\mathcode)\mathcode)="8000} \let\originalleft\left \let\originalright\right \renewcommand\left[1]{\mathopen{}\mathclose\bgroup\ifx#1(\originalleft(,\else\originalleft#1\fi} \renewcommand\right[1]{\aftergroup\egroup\ifx#1),\originalright)\else\originalright#1\fi} \makeatletter \def\resetMathstrut@{% \setbox\z@\hbox{% \mathchardef@tempa\mathcode`[\relax \def@tempb##1"##2##3{\the\textfont"##3\char"}% \expandafter@tempb\meaning@tempa \relax }% \ht\Mathstrutbox@\ht\z@ \dp\Mathstrutbox@\dp\z@} \makeatother

\begin{document} $f(x+a)$ and $\mathbf{P}\left( y = (f(x)+1)^2 \right)$. \end{document}

The output looks good to me (original above, mine below):

nicer spacing inside brackets

user21820
  • 868
  • 7
  • 19
  • @DavidCarlisle: Searching for your truncated code fragment got me the linked post, and adding that "correction" made everything work completely. There was no conflict with any of the many other packages I am using. So thanks! – user21820 Feb 20 '21 at 14:00
  • oh one of my answers:-) I still think it's a bad idea even if copied from an impeccable source:-) – David Carlisle Feb 20 '21 at 14:07
  • the space between P and ( is a well known failing of \left\right but addressed much more safely by the mleftright package – David Carlisle Feb 20 '21 at 14:10
  • @DavidCarlisle: Thanks for your feedback! It's true that meddling with internals of LaTeX is fraught with danger, but well that's precisely what my question is asking for. Heheh. =) – user21820 Feb 20 '21 at 14:16