I made some command \pcoef and \mcoef that simplifies +- and -+ to -, and -- to + in arithmetic expressions, and a \coef that simplifies 1x and -1x to x and -x respectively. They are not really satisfying, since their use is a non natural arithmetic transcription.
I would like to produce +, - and * commands that could be use in a 'natural' way.
The following code contains the commands i made, and what i would like:
\documentclass[12pt,a4paper,notitlepage]{extarticle}
\usepackage{xfp} %for \fpeval,randint
\pagenumbering{gobble}
\setlength\parindent{0pt}
% the commands i made: not very natural use
\newcommand{\coef}[1]{\ifnum\numexpr#1=1\else\ifnum\numexpr#1=-1 -\else #1\fi\fi}
\newcommand{\pcoef}[1]{\ifnum\numexpr#1=1 + \else\ifnum\numexpr#1=-1 - \else\ifnum\numexpr#1>0 + #1 \else\ifnum\numexpr#1=0 + #1 \else #1\fi\fi\fi\fi}
\newcommand{\mcoef}[1]{\ifnum\numexpr#1=1 - \else\ifnum\numexpr#1=-1 + \else\ifnum\numexpr#1>0 - #1\else + \fpeval{-#1}\fi\fi\fi}
% the commands i would like:
\newcommand{+}{} % i don't need to move margin
\renewcommand{-}{}
\renewcommand{*}{} % i don't need line break in maths
\begin{document}
% data
% later \a, \b, \c, \d will take random values, positive or negative
\edef\a{ 15 }
\edef\b{ -17 }
\def\c{ 1 }
\def\d{ -1 }
{\bf what i can do, but with non natural arithmetic transcription } \[2mm]
$\a \pcoef{\b}$ \ % a+b : 15+-17 is changed to 15-17
$\a \mcoef{\b}$ \ % a-b : 15--17 is changed to 15+17
$\coef{\a} x \pcoef{\b}$ \ % ax+b : 15x+-17 is changed to 15x-17
$\coef{\b} x \pcoef{\a}$ \ % bx+a : -17x+15 is not changed
{\bf how to correct this, for a more natural arithmetics, like $\backslash$a$\backslash$+$\backslash$b, $\backslash$a$\backslash$-$\backslash$b, $\backslash$a$\backslash$*x$\backslash$+$\backslash$b,etc } \[2mm]
$\backslash$+:\ %+ :
$\a + \b =\ $ \leftarrow the $\backslash$+ instead of + should replace +- by -. \
$\backslash$-:\ %- :
$\a - \b =\ $ \leftarrow the $\backslash$- instead of - should replace -+ by -, and -- by +. \
$\backslash$*:\ %*
%* not sure if possible, since it might regcognize an arbitraby long integer before
% an alternative would therefore be *{c}x
$\c * x =\ $\leftarrow the $\backslash$* instead of * should replace 1x by x. \
$\d x =\ $\leftarrow the $\backslash$* instead of * should replace -1x by -x. \
$\a x =\ $\leftarrow the $\backslash$* instead of * should replace a*x by ax if a is neither 1 or -1. \
\end{document}
Any help to achieve it would be appreciated.



\coefto supress the1if its argument is1or-1as well kill the*? On that note, why even use a*? Usually that means something different. Either there's no operator between a number and a variable or a\cdot. – Qrrbrbirlbel Dec 13 '22 at 14:02