# How can I draw a galvanic cell using TikZ?

Using TikZ, can we create a figure of a galvanic cell, as this picture (or fancier ;))?

I tried to create one, but apparently I'm not that good ;(

    \begin{tikzpicture}
\draw (2,6)--(2,8)--(4,8)(4,7.5)rectangle(5.5,8.5) node[midway] {$R$} (5.5,8)--(7,8)(7.5,8)circle(0.5)node{A} (8,8)--(10,8)--(10,6);
\draw (1,6)--(1,1)--(2,1)(2,1)--(5,1)(6,6)--(6,1)--(5,1)(1,5)--(6,5);

\end{tikzpicture}


Like, how can we produce those rounded corners? And by the way, in my textbook of electrochemistry, that's not a voltmeter; it's an ammeter.

This is an old drawing I have. It's a Daniell cell, but it isn't exactly like your example. I added rounded corners (obviously with rounded corners option in the \draw command), and changed the electrodes colors. The rest is what I had, but I think that you can easily change it. I use the mhchem package for the chemistry, which you probably don't need.

\documentclass[border=2mm]{standalone}
\usepackage   [version=4] {mhchem} % for the chemistry
\usepackage               {tikz}

\begin{document}
\begin{tikzpicture}[line join=round, line cap= round]
% CELLS AND ELECTRODES
\begin{scope}
\draw[thick,fill=gray!50] (0.5,1) rectangle (1,5);    % Zn electrode
\draw[thick,rounded corners] (0,4) |- (4,0) -- (4,4); % left cell
\clip[rounded corners]       (0,4) |- (4,0) -- (4,4) -- cycle;
\fill[gray,opacity=0.3]   (0,0)   rectangle (4,3);    % ZnSO4 solution
\end{scope}
\begin{scope}
\draw[thick,fill=orange!50] (8,1) rectangle (8.5,5);  % Cu electrode
\draw[thick,rounded corners] (5,4) |- (9,0) -- (9,4); % right cell
\clip[rounded corners]       (5,4) |- (9,0) -- (9,4) -- cycle;
\fill[blue, opacity=0.2]    (5,0) rectangle (9,3);    % CuSO4 solution
\end{scope}
% SALINE BRIDGE
\draw[thick] (3,1)   --++ (0,2.5) arc (180:0:1.5) --++ (0,-2.5);
\draw[thick] (3.5,1) --++ (0,2.5) arc (180:0:1)   --++ (0,-2.5);
% WIRE AND VOLTMETER
\draw[thick, rounded corners=0.5 cm] (0.75,5) |- (8.25,7) -- (8.25,5);
\begin{scope}[shift={(4.5,7)}]
\draw[thick,fill=white] (0,0) circle (0.5);
\foreach\a in{30,60,...,150}
{%
\draw[blue,thin] (\a:0.35) -- (\a:0.45);
}
\fill[blue] (0,0) circle (1pt);
\draw[blue,thick,-latex] (0,0) -- (60:0.4);
\end{scope}
% ELECTRONS
\begin{scope}[shift={(1.5,6.25)}]
\draw[red,thick,rounded corners=0.3 cm,->] (-0.5,-0.5) |- (0.5,0.5);
\end{scope}
% SIGNS
\draw[red,thick] (-0.5,5.5) circle (0.25);
\draw[red,thick] (9.5,5.5)  circle (0.25);
\draw[red,thick] (-0.7,5.5) -- (-0.3,5.5);
\draw[red,thick] (9.3,5.5)  -- (9.7,5.5);
\draw[red,thick] (9.5,5.3)  -- (9.5,5.7);
% LABELS
\node at (4.5,8)    {Voltmeter};
\node at (4.5,5.5)  {Saline bridge (\ce{KCl})};
\node at (-0.5,5)   {Anode};
\node at (-0.5,4.5) {(oxidation)};
\node at (9.5,5)    {Cathode};
\node at (9.5,4.5)  {(reduction)};
\node[red] at (1.5,6.25) {\ce{e^-}};
% CHEMISTRY
\node at (2,-0.5) {\ce{ZnSO4}};
\node at (7,-0.5) {\ce{CuSO4}};
\node at (0.75,2) {\ce{Zn}};
\node at (8.25,2) {\ce{Cu}};
\node at (0.85,2) [right] {\small\ce{-> Zn^2+}};
\node at (8.15,2) [left]  {\small\ce{Cu^2+ ->}};
\end{tikzpicture}
\end{document}


Here's an example of what you can achieve. Most of the tricks you can use to create your figure are used in this attempt at the voltmeter.

Here's the code to create this:

\documentclass[border=3pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

%% ------------------------------------------------------------
%%  A sort of one-stop-shop for controlling sizes and
%%  positions of things.
%% ------------------------------------------------------------
\def\aes{*0.75cm}
\def\aeoff{0.25\aes}

%% ------------------------------------------------------------
%%  Create some layers to facilitate coloring in the places
%%  you want colored in.
%% ------------------------------------------------------------
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\begin{document}

\begin{tikzpicture}

%% I find naming things helps keep track what you're doing.
%% otherwise this is entirely gratuitous.
\coordinate (origin) at (0,0);
\coordinate (start)  at ($(origin)+(3\aes,0)$);
\coordinate (end)    at ($(origin)-(3\aes,0)$);

%% the inner workings on the voltmeter
%% and creating two coordinates that will facilitate
%% in drawing the outer box
\draw[fill=white]
(start) -- ++ (0.25\aes,0\aes)
-- ++ (0\aes,2.75\aes)                coordinate (inrttop)
-- ++ (0\aes,-2.75\aes)               coordinate (inltbot)
-- (end)
;

%% create the outer top    right corner of the outer box
%% create the outer bottom left  corner of the outer box
\coordinate (outrttop) at ($(inrttop)+( \aeoff, \aeoff)$);
\coordinate (outltbot) at ($(inltbot)+(-\aeoff,-\aeoff)$);

%% create the gray box
%% by specifying a background layer, I don't obliterate
%% everything else I've already drawn.
\begin{pgfonlayer}{background}
\draw[fill=gray!20] (outrttop) rectangle (outltbot);
\end{pgfonlayer}

%% drawing the markings in the voltmeter
%% and singling out one in particular as the needle in red
%% ------------------------------------------------------------
%% I'm cheating here a bit.  Ideally the markings should
%% probably be drawn perpendicular to the tangent of the
%% ellipse.  That just seems like a lot of work.  And I
%% noticed that in figure you provided, it wasn't done that
%% way either.  So, perhaps not the worse cheat.
\foreach \mya in {40,70,90,100,110,140}
{
\coordinate (A\mya) at ($(origin)+(\mya:{\radiusA} and {\radiusB})$);
\ifnum\mya=100\relax
\draw[red] (A\mya) -- ++ ($(A\mya)!0.50!(origin)$);
\else
\draw (A\mya) -- ++ ($(A\mya)!0.85!(origin)$);
\fi

}

%% the label for the voltmeter
\node[anchor=south] at (origin) {Voltmeter};

\end{tikzpicture}

\end{document}


The TikZ manual is very thorough, but finding what you want can be a bit of a challenge. In recent years the manual has been more or less similarly structured with similar section headers. So, the following suggestions should be fairly reliable to get you in the appropriate section of the manual.

I'd recommend starting off by reading about the tikzlibrary for calc. A good deal of what I've done will be explained in this section of the manual.

Next you'll want to read up on the basic node and path constructors: \draw, coordinate, node, fill, etc that I've used. If you search your pdf for \path . . . node, for example, you can read up on most of these fairly easily. To find details on \draw search for /tikz/draw and \draw (the former will give you most of the necessary details, \draw will just tell you what it's short-hand for).

The use of -- can be found under line-to operation

The use of rectangle can be found under rectangle operation

A bit tricky to find is the ++ notation. For this, search for Specifying Coordinates.

There's also a section on Arc Operations that will explain the use of the arc(<start angle>:<end angle>:<radius A> and <radius B>) syntax.

There's also a whole section on Using Layers to get a feel for what layers are about and what you can do with them.

• Personally, I find working with arc a bit awkward, so for the beakers I would acutally suggest something along the lines of \draw[thick] (0,0) -- ++(-2,0) to[out=180,in=270] ++(-1.5,1.5);. Here to replaces -- and the numbers define the angles of the line at the beginning and end point. Apr 15, 2021 at 7:30