Laurenso
I have an expression x^2 - a x - b x + ab. Now I want to substitute a = 1 and b = 2 to get x^2 - x - 2x + 2. How can I get the result?

\documentclass[12pt,a4paper]{article}
\usepackage{mathtools}
\usepackage{amsthm}
\usepackage{calc}
\begin{document}
\pgfmathsetmacro{\a}{1}
\pgfmathsetmacro{\b}{2}
x^2 - \a*x - \b*x + \a*\b
\end{document}

I can not get the result x^2 - x - 2x + 2.
samcarter
You could wrap your coefficients into a macro to evaluate them:


\documentclass{article}

\usepackage{pgf}

\pgfkeys{/pgf/number format/print sign=true}

\newcommand{\foo}[1]{%
\pgfmathparse{#1}%
\pgfmathprintnumber{\pgfmathresult}
}

\begin{document}
\pgfmathsetmacro{\a}{1}
\pgfmathsetmacro{\b}{2}
$x^2 \foo{-\a}x \foo{-\b}x \foo{+\a*\b}$
\end{document}


![Screenshot 2023-07-23 at 15.12.35.png](/image?hash=aea97c022dc5f2225b8df4a44e75f78f4f321c04aefecfbdc2ce744eea34fc9d)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.