Edit and compile if you like:
% Author: Izaak Neutelings (July 2018)\documentclass[border=3pt,tikz]{standalone}\usepackage{amsmath}\usepackage{tikz}\usepackage{physics}\tikzset{>=latex} % for LaTeX arrow head\usepackage{xcolor}\colorlet{BFcol}{red!60!black}\tikzstyle{force}=[->,thick,BFcol]\def\a{2.1}\def\F{1.0}\tikzset{pics/magnet/.style={ %args={#1}code={\def\h{0.8}\coordinate (-N) at (0,\h);\coordinate (-S) at (0,-\h);\draw[pic actions,thick,top color=red!60,bottom color=red!90,shading angle=20](-0.8*\h/2,0) rectangle ++(0.8*\h,\h);\draw[pic actions,thick,top color=blue!60,bottom color=blue!90,shading angle=20](-0.8*\h/2,0) rectangle ++(0.8*\h,-\h);\node[pic actions] at (0, \h/2) {\textbf{N}};\node[pic actions] at (0,-\h/2) {\textbf{S}};}},pics/nail/.style={code={\def\t{0.08}\def\L{1.2}\def\w{0.16}\def\h{0.08}\coordinate (-N) at (\L/2+\h,0);\coordinate (-S) at (0,0);\draw[pic actions,thick,top color=black!20,bottom color=black!50,shading angle=20](\L/2,\t/2) --++ (0,-\t) --++ (-\L,0) --++ (-0.14*\L,\t/2) --++ (0.14*\L,\t/2) -- cycle;\draw[pic actions,thick,top color=black!20,bottom color=black!50,shading angle=20](\L/2,-\w/2) rectangle ++(\h,\w) -- cycle;}}}
Click to download: magnetic_force.tex • magnetic_force.pdf
Open in Overleaf: magnetic_force.tex