# TikZ Make a graphical balance for equations

I'd like to be able to draw a white = inside of the triangle, be able to rotate the balance and the equation and place the equations symmetrical yet not to close to the edge. It's quite hard for me.

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}
\begin{center}
$2+2$ \hfill $4$
\begin{tikzpicture}
\draw[red!50!blue!70!white,ultra thick](-5.4,1) -- (5.4,1);
\fill[red!50!blue!70!white] (0,0.98) -- (-0.4,0) -- (0.4,0);
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}


See the update with a quick generating command at the end of this post.

The center of a triangle could be easily obtained with the barycentric coordinate system provided by TikZ.

Define the three corner nodes of the triangle and say

\node[color=white]  at (barycentric cs:A=1,B=1,C=1) {=};


in order to use weighted vertices (here all having the same weight, which corresponds to the true barycenter of the triangle then.

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{frame}
\begin{center}
$2+2$ \hfill $4$
\begin{tikzpicture}
\coordinate (A) at (0,0.98);
\coordinate (B) at (-0.4,0);
\coordinate (C) at (0.4,0);
\draw[red!50!blue!70!white,ultra thick](-5.4,1) -- (5.4,1);
\fill[red!50!blue!70!white] (A) -- (B) -- (C);
\node[color=white]  at (barycentric cs:A=1,B=1,C=1) {=};
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}


Update

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{frame}
\begin{center}
\begin{tikzpicture}
\def\winkel{20}
\coordinate (A) at (0,0.98);
\coordinate (B) at (-0.4,0);
\coordinate (C) at (0.4,0);
\coordinate (rodleft) at (-5.4,1);
\coordinate (rodright) at (5.4,1);
\coordinate (balancetop) at (0,1);
\coordinate (rotatedleft) at ($(balancetop)-(\winkel:5)$);
\coordinate (rotatedright) at ($(balancetop)+(\winkel:5)$);
\draw[red!50!blue!70!white,ultra thick] (balancetop) -- (rotatedleft);
\draw[red!50!blue!70!white,ultra thick] (balancetop) -- (rotatedright);
\node[rotate=\winkel,above right] (LHS) at ($(rotatedleft) + (\winkel:1)$) {$2+2$};
\node[rotate=\winkel,above left] (RHS) at ($(rotatedright) - (\winkel:1)$) {$3$};
\fill[red!50!blue!70!white] (A) -- (B) -- (C);
\node[font=\large,color=white]  at (barycentric cs:A=1,B=1,C=1) {$\neq$};
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}


2nd update

The macro \equationbalance checks the arguments #3 and #4 and prints either = or \neq, however, the angle must be specified still. I don't find it useful to calculate the angle from the values of #3 or #4 (from the Physics view: the angle would be not constant anyway, if there both sides of the scale hold unequal values)

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{calc}

% First opt arg: angle
% 2nd arg: inward shift of the labels
% 3rd arg: left hand label
% 4th arg: right hand label
\newcommand{\equationbalance}[4][0]{%
\begin{tikzpicture}
\def\winkel{#1}
\coordinate (A) at (0,0.98);
\coordinate (B) at (-0.4,0);
\coordinate (C) at (0.4,0);
\coordinate (rodleft) at (-5.4,1);
\coordinate (rodright) at (5.4,1);
\coordinate (balancetop) at (0,1);
\coordinate (rotatedleft) at ($(balancetop)-(\winkel:5)$);
\coordinate (rotatedright) at ($(balancetop)+(\winkel:5)$);
\draw[red!50!blue!70!white,ultra thick] (balancetop) -- (rotatedleft);
\draw[red!50!blue!70!white,ultra thick] (balancetop) -- (rotatedright);
\node[rotate=\winkel,above right] (LHS) at ($(rotatedleft) + (\winkel:#2)$) {$#3$};
\node[rotate=\winkel,above left] (RHS) at ($(rotatedright) - (\winkel:#2)$) {$#4$};
\fill[red!50!blue!70!white] (A) -- (B) -- (C);
% Works for simple integer expressions only
\ifnum\numexpr#3=#4
\node[font=\large,color=white]  at (barycentric cs:A=1,B=1,C=1) {$=$};
\else
\node[font=\large,color=white]  at (barycentric cs:A=1,B=1,C=1) {$\neq$};
\fi
\end{tikzpicture}%
}

\begin{document}
\begin{frame}
\begin{center}
\equationbalance[10]{1.5}{2+2}{3}
\end{center}
\end{frame}
\end{document}


• Nice! Could one move the text somehow a little bit inwards? It would be nice to get say three spots left and right where I can add x and y's. So they stay where they are. – Joel Duscha May 6 '17 at 19:14
• @JoelDuscha: You mean 2+2 should go more to the left and 4 more the right? – user31729 May 6 '17 at 19:31
• the other way around, please – Joel Duscha May 6 '17 at 19:32
• @JoelDuscha: Yes of course, I was confused – user31729 May 6 '17 at 19:35
• Always good to ask. I'm certainly more often confused and you already helped :D – Joel Duscha May 6 '17 at 19:42

With the new TABcline option to tabstackengine (2018/03/05 (V2.10)), this becomes a nice exercise. The support macros \balanceindent and \lever control the indent of text from the ends of the lever and the lever to fulcrum distance, respectively. The lever thickness is controlled by \fboxrule which is defined locally inside the main macro. The main routine is

\balance[fulcrum inset]{left terms}{right terms}


The default fulcrum inset is ?. If it is set to > or <, the lever will be made to tilt in the appropriate direction.

Here's the MWE:

\documentclass{article}
\usepackage[TABcline]{tabstackengine}
\usepackage{graphicx}
\newcommand\Leqtriangle[1][=]{%
\stackengine{2pt}{\Ltriangle}{$\scriptstyle#1\mkern1mu$}{O}{c}{F}{F}{L}}
\newcommand\Ltriangle{\LARGE$\triangle$}
\newcommand\balanceindent{\kern10pt}
\newcommand\lever{\kern7em}
\newcommand\balance[3][?]{{%
\TABbinary\TABstackMath\setstackgap{S}{0pt}\fboxrule=1pt\relax%
\def\bangle{4}% TILT ANGLE
\def\bsign{}%
\ifx>#1\def\bsign{-}\else\ifx<#1\else\def\bangle{0}\fi\fi%
\rotatebox[origin=b]{\bsign\numexpr-\bangle}{%
\tabularShortstack{lcr}{\mathstrut\balanceindent#2 && #3\balanceindent\\%
\TABcline{1-3}\lever &%
\rotatebox[origin=t]{\bsign\numexpr\bangle}{\Leqtriangle[#1]} & \lever}}}}
\begin{document}
\balance[>]{2 + 2(4-3)}{6 - 6/2}

\balance[=]{2 + 2}{4}

\balance[<]{2 + 3}{7}

\balance{y}{42}
\end{document}