1

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:

\newcommand\coefficient{1.25}

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:

\documentclass[border=10pt,convert={density=300,size=1080x800,outext=.png}]{standalone} 
\input{coefficients.tex}
\usepackage{amsmath}
\begin{document}
$\displaystyle
y(k) = 
\ifdim\dimexpr\coefficient pt =0pt
\else
\ifdim\dimexpr\coefficient pt >0pt
+ 
\else 
\fi
\ifdim\dimexpr\coefficient pt=1pt
\else
\ifdim\dimexpr\coefficient pt=-1pt
-
\else
\coefficient
\fi
\fi
x(k-1) 
\fi
+ 5
$
\end{document}

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
2

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.

\documentclass{article}


\makeatletter
\def\printcoefficient#1#2{%
    \ifdim\dimexpr#1pt=0pt\else
        \ifdim\dimexpr#1 pt >0pt%
            +%
        \fi
        \ifdim\dimexpr#1pt=1pt\else
            \ifdim\dimexpr#1pt=-1pt%
                -%
            \else
                \expandafter\pc@removepoint\the\dimexpr#1pt%
            \fi
        \fi
        #2%
    \fi
}
\def\printnegativecoefficient#1{\printcoefficient{-#1}}

\begingroup\lccode`8=`p\lccode`9=`t\lowercase{\endgroup
    \def\pc@removepoint#189{#1}
}
\makeatother



\begin{document}

$1\printcoefficient{0}{x}$

$1\printcoefficient{1}{x}$

$1\printcoefficient{2}{x}$

$1\printcoefficient{-1}{x}$

$1\printcoefficient{-1.2}{x}$

$1\printcoefficient{1.2}{x}$


$1\printnegativecoefficient{0}{x}$

$1\printnegativecoefficient{1}{x}$

$1\printnegativecoefficient{2}{x}$

$1\printnegativecoefficient{-1}{x}$

$1\printnegativecoefficient{-1.2}{x}$

$1\printnegativecoefficient{1.2}{x}$
\end{document} 
3

You could do it without additional packages, but why?

\documentclass[border=10pt]{standalone} 
\usepackage{amsmath}
\usepackage{xfp} % also loads expl3

\ExplSyntaxOn
\NewExpandableDocumentCommand{\cf}{sm}
 {
  \fp_compare:nF { #2 < \c_zero_fp } { \IfBooleanF { #1 } { + } }
  \fp_compare:nF { #2 = \c_one_fp }
   {
    \fp_compare:nTF { #2 = -\c_one_fp }
     { - }
     { #2 }
   }
 }
\ExplSyntaxOff

\newcommand{\constA}{-2.5}
\newcommand{\constB}{1.35}
\newcommand{\constC}{1}
\newcommand{\constD}{-1}

\begin{document}

$\displaystyle
y(k) =
\cf{\constA}x(k-1)
\cf{\constB}x^2
\cf{\constC}x^3
\cf{\constD}x^4
\qquad
z(k) =
\cf*{\constC}{x^3}
$

\end{document}

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.