I am setting up a document that outputs an equation, with some parts showing or not showing depending on commands i set in a different file.

the commands look like this:


I want this coefficient to show in the equation if it is non-zero and not 1 or -1. If it is positive, it should output as -1.25 (change sign). So far, I'm doing it like this:

y(k) = 
\ifdim\dimexpr\coefficient pt =0pt
\ifdim\dimexpr\coefficient pt >0pt
\ifdim\dimexpr\coefficient pt=1pt
\ifdim\dimexpr\coefficient pt=-1pt
+ 5

What this does is displaying the value when it is not 0, 1, or -1, showing a - if it is -1, and not showing the x(k-1) part in case the value is 0.

Now for my equation, i would like to change the sign so that every positive value defined in the command outputs as negative in the equation and every negative value outputs as positive (which is harder).

How can i do this (preferrably without using additional packages) ?

  • 2
    You can't define a command with a number in its name. If I create a one-liner coefficients.tex and try your code, I get ! LaTeX Error: Missing \begin{document}.. – egreg Feb 10 '18 at 10:33

Here's a solution that does what you want. Unfortunately, it's not possible to do floating point math with a sane input scheme without loading a package, so this command can't do any arithmetic.


        \ifdim\dimexpr#1 pt >0pt%















You could do it without additional packages, but why?

\usepackage{xfp} % also loads expl3

  \fp_compare:nF { #2 < \c_zero_fp } { \IfBooleanF { #1 } { + } }
  \fp_compare:nF { #2 = \c_one_fp }
    \fp_compare:nTF { #2 = -\c_one_fp }
     { - }
     { #2 }



y(k) =
z(k) =


The *-version is for the first coefficient to print: if it is positive, the + sign should be omitted.

enter image description here

  • Much better than my answer, but I actually listened to him. =P – Hood Chatham Feb 10 '18 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.