Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is there a way to fill this glass tube with different quantities of red color? I'm looking for a similar solution found in the thermometer problem (Fill of a thermometer).

My minimal example is:

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
\draw (0,0) ellipse (1 and .5);
\draw (-1,0)--(-1,-5);
\draw (1,0)--(1,-5);
\draw (-1,-5) arc (180:360:1);

\foreach \y/\x in {-5/1,
                   -4/2,
                   -3/3,
                   -2/4%
                   }
    {
    \draw (-0.2,\y)--(0.2,\y) node[right](\x){\x};

    \foreach \z in {0.2,0.4,0.6,0.8}
                    {\draw ($(-0.1,\z) + (0,\y)$)--($(0.1,\z)+(0,\y)$);}
    };


\end{tikzpicture}

\end{document}
share|improve this question
1  
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  Jubobs Dec 9 '13 at 23:27
add comment

5 Answers

A little make-up with slight modifications

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
\shade[left color=red,right color=red!40] 
    (-1,-2)--(-1,-5) arc (180:360:1)  -- (1,-2) arc (0:180:1 and 0.3);
\draw (0,0) ellipse (1 and .3);
\draw (-1,0)--(-1,-5) arc (180:360:1) --(1,-5) -- (1,0);
\draw[red!90!black!70] (0,-2) ellipse (1 and .3);

\foreach \y/\x in {-5/1,-4/2,-3/3,-2/4}
    {
    \draw (-0.2,\y) to[bend right=10](0.2,\y) node[right,yslant=0.15](\x){\x};

    \foreach \z in {0.2,0.4,0.6,0.8}
       \draw ($(-0.1,\z) + (0,\y)$)to[bend right=5]($(0.1,\z)+(0,\y)$);
    };
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
4  
too lazy for surface tension and colorblind for the surface edge –  percusse Dec 9 '13 at 23:35
add comment

With adjustable height:

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}    

\begin{document}
\newlength{\mylen}
\setlength{\mylen}{-3cm}                %% change here for changing length (height)
\begin{tikzpicture}
\draw (0,0) ellipse (1 and .5);
\draw (-1,0)--(-1,-5);
\draw (1,0)--(1,-5);
\draw (-1,-5) arc (180:360:1);

\fill[red!50] (-1,\mylen)--(-1,-5) arc (180:360:1) -- (1,\mylen) --cycle;
\path[fill=red!40!white,] (0,\mylen) ellipse (1 and .5);       

\foreach \y/\x in {-5/1,
                   -4/2,
                   -3/3,
                   -2/4%
                   }
    {
    \draw (-0.2,\y)--(0.2,\y) node[right](\x){\x};

    \foreach \z in {0.2,0.4,0.6,0.8}
                    {\draw ($(-0.1,\z) + (0,\y)$)--($(0.1,\z)+(0,\y)$);}
    };


\end{tikzpicture}

\end{document}

enter image description here

Obligatory animation:

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\foreach \h in {-1.2,-1.4,...,-5}{
\begin{tikzpicture}
\draw (0,0) ellipse (1 and .3);
\draw (-1,0)--(-1,-5);
\draw (1,0)--(1,-5);
\draw (-1,-5) arc (180:360:1);

\fill[red!50] (-1,\h)--(-1,-5) arc (180:360:1) -- (1,\h) --cycle;
\path[fill=red!40!white,] (0,\h) ellipse (1 and .3);

\foreach \y/\x in {-5/1,
                   -4/2,
                   -3/3,
                   -2/4%
                   }
    {
    \draw (-0.2,\y)--(0.2,\y) node[right](\x){\x};

    \foreach \z in {0.2,0.4,0.6,0.8}
                    {\draw ($(-0.1,\z) + (0,\y)$)--($(0.1,\z)+(0,\y)$);}
    };


\end{tikzpicture}
}
\end{document}

enter image description here

share|improve this answer
add comment

Two separate solutions.

Code

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
        \fill[color=red!80] (-1,-4) arc (180:0:1 and .2) -- (1,-5) arc (0:-180:1);
        \fill[color=red] (0,-4) ellipse (1 and .2);%Not necessary, it is there if the opacity is changed or a solution deleted ;)
        \fill[color=red!35] (-1,-2) arc (180:0:1 and .2) -- (1,-4) arc (0:-180:1 and .2);
        \fill[color=red!50] (0,-2) ellipse (1 and .2);
        \foreach \y/\x in {-5/1,-4/2,-3/3,-2/4}{%
            \draw (-0.2,\y) -- (0.2,\y) node[right](\x){\x};
            \foreach \z in {0.2,0.4,0.6,0.8}
               \draw ($(-0.1,\z) + (0,\y)$) -- ($(0.1,\z)+(0,\y)$);
        };
        \draw[thick] (1,0) -- (1,-5) arc (0:-180:1) -- (-1,0);
        \draw[thick] (0,0) ellipse (1 and .2);
    \end{tikzpicture}
\end{document}

Result

enter image description here

share|improve this answer
add comment

Joining late the party (the right occasion to use \pgfmathdeclareoperator).

Key aspects of the answer:

  1. drawing the glass tube is simpler at user-side as all the relevant code has been gathered by the glass tube style; options have been introduced to customize size of the tube; the scale is shown accordingly to customization provided;
  2. added upper tick value to the indicators;
  3. possibility to select the amount of liquid providing a ml-based measure: liquid=4ml.

The code:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc}

\tikzset{glass tube height/.store in=\glasslength,
 glass tube height=5,
 glass tube width/.store in=\glasswidth,
 glass tube width=1,
 glass tube/.style={glass indicators,
   insert path={
    (-\glasswidth,0)--(-\glasswidth,-\glasslength)arc (180:360:\glasswidth)
    (\glasswidth,-\glasslength)--(\glasswidth,0)
    (0,0) ellipse (\glasswidth{} and .5\glasswidth)
   }
 },
 glass indicators/.code={
   \pgfmathtruncatemacro\xglasslength{\glasslength -1}
    \foreach \y[count=\x] in {-\glasslength,-\xglasslength,...,-1}
    {
    \draw (-0.2,\y)--(0.2,\y) node[right](\x){\x};
    \ifnum\y<-1
    \foreach \z in {0.2,0.4,0.6,0.8}
                    {\draw ($(-0.1,\z) + (0,\y)$)--($(0.1,\z)+(0,\y)$);}
    \fi
    };
 },
 liquid color/.code={\tikzset{liquid n/.style={top color=#1!10,bottom color=#1},liquid u/.style={#1!13}}},
 liquid/.style={
   insert path={
   \pgfextra{
    \fill[liquid n](-\glasswidth,#1)--(-\glasswidth,-\glasslength)arc (180:360:\glasswidth)
    (\glasswidth,-\glasslength)--(\glasswidth,#1)--(-\glasswidth,#1);
    \fill[liquid u](0,#1) ellipse (\glasswidth{} and .5\glasswidth);
    }
   },
 }
}

% code by Mark Wibrow:
% http://tex.stackexchange.com/a/124177/13304
\pgfmathdeclareoperator{ml}{milliliters}{2}{postfix}{600}
\pgfmathdeclarefunction{milliliters}{1}{%
    \begingroup%
        \pgfmathparse{#1-1-\glasslength}%
        \expandafter%
    \endgroup\expandafter\edef\expandafter\pgfmathresult\expandafter{\pgfmathresult}%
}

\begin{document}

\begin{tikzpicture}
\draw[glass tube];

\begin{scope}[on background layer]
\fill[liquid color=red,liquid=4ml];
\end{scope}
\end{tikzpicture}

\end{document}

The result:

enter image description here

Animated version (with tube customization):

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc}

\tikzset{glass tube height/.store in=\glasslength,
 glass tube height=5,
 glass tube width/.store in=\glasswidth,
 glass tube width=1,
 glass tube/.style={glass indicators,
   insert path={
    (-\glasswidth,0)--(-\glasswidth,-\glasslength)arc (180:360:\glasswidth)
    (\glasswidth,-\glasslength)--(\glasswidth,0)
    (0,0) ellipse (\glasswidth{} and .5\glasswidth)
   }
 },
 glass indicators/.code={
   \pgfmathtruncatemacro\xglasslength{\glasslength -1}
    \foreach \y[count=\x] in {-\glasslength,-\xglasslength,...,-1}
    {
    \draw (-0.2,\y)--(0.2,\y) node[right](\x){\x};
    \ifnum\y<-1
    \foreach \z in {0.2,0.4,0.6,0.8}
                    {\draw ($(-0.1,\z) + (0,\y)$)--($(0.1,\z)+(0,\y)$);}
    \fi
    };
 },
 liquid color/.code={\tikzset{liquid n/.style={top color=#1!10,bottom color=#1},liquid u/.style={#1!13}}},
 liquid/.style={
   insert path={
   \pgfextra{
    \fill[liquid n](-\glasswidth,#1)--(-\glasswidth,-\glasslength)arc (180:360:\glasswidth)
    (\glasswidth,-\glasslength)--(\glasswidth,#1)--(-\glasswidth,#1);
    \fill[liquid u](0,#1) ellipse (\glasswidth{} and .5\glasswidth);
    }
   },
 }
}

% code by Mark Wibrow:
% http://tex.stackexchange.com/a/124177/13304
\pgfmathdeclareoperator{ml}{milliliters}{2}{postfix}{600}
\pgfmathdeclarefunction{milliliters}{1}{%
    \begingroup%
        \pgfmathparse{#1-1-\glasslength}%
        \expandafter%
    \endgroup\expandafter\edef\expandafter\pgfmathresult\expandafter{\pgfmathresult}%
}

\begin{document}

\foreach \level in {1,...,9}{
\begin{tikzpicture}[glass tube height=9,glass tube width=2]
\draw[glass tube];

\begin{scope}[on background layer]
\fill[liquid color=red,liquid=\level ml];
\end{scope}
\end{tikzpicture}
}

\end{document}

enter image description here

share|improve this answer
add comment

Solved

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\begin{tikzpicture}
\draw (0,0) ellipse (1 and .5);
\draw (-1,0)--(-1,-5) arc (180:360:1)--(1,0);

\foreach \y/\x in {-5/1,
                   -4/2,
                   -3/3,
                   -2/4%
                   }
    {
    \draw (-0.2,\y)--(0.2,\y) node[right](\x){\x};

    \foreach \z in {0.2,0.4,0.6,0.8}
                    {\draw ($(-0.1,\z) + (0,\y)$)--($(0.1,\z)+(0,\y)$);}
    };

\filldraw[fill opacity=0.5,fill=red](-1,-2)--(-1,-5) arc (180:360:1)--(1,-2);

\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
Adding a meniscus: \filldraw[fill opacity=0.5,fill=red](-1,-2.2)--(-1,-5) arc (180:360:1)--(1,-2.2) to [out=200,in=340] (-1,-2.2); –  Jorge Dec 9 '13 at 23:24
    
A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). –  Jubobs Dec 9 '13 at 23:26
12  
it looks like you've used code from the answers to produce your answer. It's best to thank people for their time and energy by upvoting helpful answers- if one answer solved the problem, you can accept it by using the green check mark. Welcome to the site! –  cmhughes Dec 10 '13 at 0:01
2  
@Jorge: Using a different perspective for the top of the glass than for the surface of the liquid is really confusing. I would suggest you go with one of the other solutions, vote for all answers that you find helpful, and accept the most useful one. –  Jake Dec 10 '13 at 10:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.