1

Consider the following MnotWE:

\documentclass[12pt,letterpaper]{article}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{calc}

\newcommand{\scaleandcenter}[2]{\vcenter{\hbox{\scalebox{#1}{$#2$}}}}
\newcommand{\binscaled}[2]{\mathbin{\scaleandcenter{#1}{#2}}}
\newcommand{\smallbin}[1]{\binscaled{0.5}{#1}}

\newcommand{\setbinprecedence}[2]{\setlength{\medmuskip}{(1 + #1)*1mu plus (1 + #1)*1mu minus (1 + #1)*1mu} {#2}}

\let\origtimes\times
\renewcommand{\times}{\setbinprecedence{0}{\smallop{\origtimes}}}

\begin{document}
    \begin{equation*}
        a\times b + c\times b = 0
    \end{equation*}
\end{document}

It produces errors, because it doesn't like how I am using arguments in the arithmetic within the \setlength command. How do I properly pass an argument as a number for use within a calc package command?

Purpose: I would like to set up a kind of "visual precedence" style, where operands for my custom \times are tighter around it than the operands around the +, because \times should have "higher precedence" than +. Another way to say it: "visually" the operands around times should be grouped tightly around it, to pass on intuitively the notion that times should be performed first. So I was hoping to set custom medmuskips (including stretches and shrinks) depending on the precedence level of the operator.

bzm3r
  • 3,196
  • the error is undefined command \setvalue simply because \setvalue has not been defined. the calc package is not involved in the error. – David Carlisle Sep 17 '17 at 22:14
  • Even if the correct \setlength command worked (it doesn't with mu lengths with the calc package), this would not do: TeX uses a single value for \medmuskip in the whole formula, namely the one in force at the end of the formula. Besides, \times should be a \mathbin, not a \mathop. – egreg Sep 17 '17 at 22:17
  • apart from \setvalue not being defined, it is not clear what the intention of \setbinprecedence is, it makes a {..} group so will always be a mathord and the inner setting of \mathop will be lost. – David Carlisle Sep 17 '17 at 22:18
  • @DavidCarlisle I fixed that! You're right. I'd like to redefine the \medmuskip just for a particular command. – bzm3r Sep 17 '17 at 22:27
  • after the edit the error is now Illegal unit of measure but this is unrelated to passing arguments through \newcommand you can not use calc syntax with mu units, and in anycase changing \medmuskip at that point would have no effect. – David Carlisle Sep 17 '17 at 22:27

1 Answers1

2

The calc package doesn't work with muglue expressions. You might do without it by saying

\setlength{\medmuskip}{%
  \muexpr (1+#1)mu plus (1+#1)mu minus (1+#1)mu\relax
}

but this wouldn't work for the reason you're doing the setting in a group and making (the redefined) \times an ordinary atom.

However, there is a much stronger reason: TeX uses just one value of \medmuskip across a formula, namely the one that holds at the end of the formula.

Examples:

\documentclass{article}

\begin{document}

$a+b$

$a+b\medmuskip=30mu$

${\medmuskip=30mu a+b}$

\end{document}

You see that setting \medmuskip after the plus sign has been digested will result in enlarged space anyway. Similarly, setting the parameter in a group is of no consequence, because the value at the end of the formula will be the initial one.

enter image description here

If you want to make spaces tighter around a binary operator than the standard, remove a fraction of \medmuskip. For instance

\documentclass{article}
\usepackage{amsmath}

\newcommand{\tighttimes}{%
  \mspace{-\muexpr\medmuskip/2}
  \times
  \mspace{-\muexpr\medmuskip/2}
}
\begin{document}

$a+b\tighttimes c$

\medmuskip=30mu

$a+b\tighttimes c$

\end{document}

The second formula uses an exaggerated value of \medmuskip to better show the effect.

enter image description here

egreg
  • 1,121,712
  • Thanks for this. I would like to set up a kind of "visual precedence" style, where operands for my custom \times are tighter around it than the operands around the +, because \times should have "higher precedence" than +. Does that make sense? So I was hoping to set custom medmuskips (including stretches and shrinks) depending on the precedence level of the operator. – bzm3r Sep 17 '17 at 22:31