4

Following up this answer, I would like to create a \num-based command that nullifies the white space from the left side of a binary operator and keeps the space on its right side.

In other words, I need to keep the minus sign, in the first line of the example below, aligned with the page margin while having a white space between it and number 3 as that surrounding the minus sign of the second line.

enter image description here

\documentclass{article}

\usepackage{siunitx}
\usepackage[showframe]{geometry}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\snum}{O{}m}
{
    \mathbin
    {
        \fp_compare:nTF { #2 >= 0 }
        { + }
        { - }
    }
    \num[#1]{ \fp_eval:n { abs(#2) } }
}

\ExplSyntaxOff

\parindent=0pt

\begin{document}
    \def\numb{-3}
    
    $\snum{\numb}$\\
    ${}\snum{\numb}$
\end{document}

2 Answers 2

3

The macro \mynum, defined in this answer, may be of interest to you.

enter image description here

\documentclass{article}
\usepackage{showframe} % draw framelines around text block
\setlength\parindent{0pt}
\usepackage{siunitx} % for '\num' macro
\usepackage{amsmath} % for '\ensuremath' macro

\newcommand\mynum[1]{\ensuremath{\mskip-\medmuskip{}\num[retain-explicit-plus]{#1}}}

\begin{document}
\obeylines % just for this example
\num{-3}
\num[retain-explicit-plus]{+8}
$\mskip-\medmuskip{}-3$
\mynum{-3}
\mynum{+8}
\end{document}
5

enter image description here

a mathbin has \medmuskip either side, so you either want a mathbin with -\medmuskip on the left, or a \mathord with \medmuskip on the right. I do the first here.

\documentclass{article}

\usepackage{siunitx}
\usepackage[showframe]{geometry}

\ExplSyntaxOn

\NewExpandableDocumentCommand{\snum}{O{}m}
{
    \mathbin
    {
        \fp_compare:nTF { #2 >= 0 }
        { + }
        { - }
    }
    \num[#1]{ \fp_eval:n { abs(#2) } }
}

\ExplSyntaxOff

\parindent=0pt

\begin{document}
    \def\numb{-3}
    
    $\snum{\numb}$\\
    ${}\snum{\numb}$\\
    ${}\mskip-\medmuskip\snum{\numb}$
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .