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.

I am a beginner in Tikz and I have two questions:

  1. I'm trying to draw the picture below but have been unsuccessful (I drew the right part though but couldn't draw the left one)
  2. How can I reuse these two shapes in other pictures if I use them a lot?

This is what I used to draw the right part:

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

\begin{document}
\begin{tikzpicture}
    \draw[semithick] (0,0) -- (1,4);% left line
    \draw[semithick] (3,0) -- (2,4);% right line
    \draw[semithick] (0,0) arc (180:0:1.5 and -0.5);
    \draw[semithick] (1.5,4) ellipse (0.5 and 0.166);
\end{tikzpicture}
\end{document}

enter image description here

I appreciate your help and thank you in advance.

share|improve this question

3 Answers 3

As Harish Kumar points out "pics" can be useful for reusing small pictures. The following was took a bit of trial an error but sort of looks like the OPs required image.

\documentclass[border=10]{standalone}
\usepackage{tikz}

\tikzset{mobile phone/.pic={
\code={
\begin{scope}[line join=round,looseness=0.25, line cap=round]
\begin{scope}
\clip [preaction={left color=blue!10, right color=blue!30}] 
  (1/2,-1) to [bend left] (0,10)
  to [bend left] ++(1,1) -- ++(0,2)
  arc (180:0:3/4 and 1/2) -- ++(0,-2)
  to [bend left]  ++(5,-2) coordinate (A) to [bend left] ++(-1/2,-11)
  to [bend left] ++(-1,-1) to [bend left] cycle;
%
\path [left color=blue!30, right color=blue!50]
  (A) to [bend left] ++(0,-11) to[bend left] ++(-3/2,-2)
  -- ++(0,12);
%
\path [fill=blue!20, draw=white, line width=0.25cm]
  (0,10) to [bend left] ++(1,1) -- ++(0,2)
  arc (180:0:3/4 and 1/2) -- ++(0,-2)
  to [bend left]  (A) to [bend left] ++(-3/2,-5/4)
  to [bend right] cycle;
%
\draw [line width=0.25cm, fill=white]
  (9/8,21/2) arc (180:360:5/8 and 3/8) --
  ++(0,2.5) arc (0:180:5/8 and 3/8) -- cycle;
%
\draw [line width=0.25cm, fill=white]
  (9/8,13) arc (180:360:5/8 and 3/8);
%
\fill [white, shift=(225:0.5)] 
  (1,17/2) to [bend left] ++(4,-7/4)
  to [bend left] ++(0,-7/2) to [bend left] ++(-4, 6/4)
  to [bend left] cycle;
%
\fill [black, shift=(225:0.25)] 
  (1,17/2) to [bend left] ++(4,-7/4)
  to [bend left] ++(0,-7/2) to [bend left] ++(-4, 6/4)
  to [bend left] cycle;
%
\shade [inner color=white, outer color=cyan!20] 
  (1,17/2) to [bend left] ++(4,-7/4)
  to [bend left] ++(0,-7/2) to [bend left] ++(-4, 6/4)
  to [bend left] cycle;
\draw [line width=0.25cm, shorten <=-0.5cm, shorten >=-0.5cm]
  (1,17/2) to [bend left] ++(4,-7/4)
  to [bend left] ++(0,-7/2);
%
\foreach \i in {1,2,3}
  \foreach \j in {1,2,3}
     \draw [line width=0.125cm, top color=gray, 
       bottom color=white, yslant=-3/8, rounded corners=1cm/8] 
         (\i*3/2-1/2,-\j+4) rectangle ++(1,3/4);
%
\draw [line width=0.125cm, top color=gray, bottom color=white, yslant=-3/8] 
  (3,0) ellipse [x radius=1/2, y radius=1/3];
%
\draw [line width=0.125cm, top color=gray, bottom color=white, yslant=-3/8] 
  (3,19/2) ellipse [x radius=1, y radius=1/4];
\end{scope}
\draw [line width=0.5cm] 
  (1/2,-1) to [bend left] (0,10)
  to [bend left] ++(1,1) -- ++(0,2)
  arc (180:0:3/4 and 1/2) -- ++(0,-2)
  to [bend left]  ++(5,-2) to [bend left] ++(-1/2,-11)
  to [bend left] ++(-1,-1) to [bend left] cycle;
\end{scope}%
}}}
\begin{document}

\begin{tikzpicture}
\path (0,0) pic {mobile phone};
\end{tikzpicture}
\end{document}

enter image description here

And here's a (somewhat excessive) mobile phone image I created for a presentation last year. Because it was reused on several slides, I just compiled it using the standalone class as phone.pdf and whenever I needed it, I used:

\includegraphics{phone.pdf}

The following requires lualatex to compile as I made use of os.date to update the date and time whenever the document was compiled.

\documentclass[border=10]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}

% UCL colors
\definecolor{Dark Blue}{RGB}{0,67,89}
\definecolor{Navy Blue}{RGB}{0,69,124}
\definecolor{Blue}{RGB}{0,70,120}
\definecolor{Mid Blue}{RGB}{84,131,164}
\definecolor{Bright Blue}{RGB}{69,156,189}
\definecolor{Light Blue}{RGB}{168,192,209}
\definecolor{Dark Green}{RGB}{75,70,32}
\definecolor{Mid Green}{RGB}{130,141,55}
\definecolor{Bright Green}{RGB}{204,209,88}
\definecolor{Light Green}{RGB}{185,193,147}
\definecolor{Dark Purple}{RGB}{82,66,91}
\definecolor{Light Purple}{RGB}{191,175,188}
\definecolor{Pink}{RGB}{145,24,83}
\definecolor{Dark Red}{RGB}{90,27,49}
\definecolor{Red}{RGB}{193,0,68}
\definecolor{Rich Red}{RGB}{237,23,79}
\definecolor{Mid Red}{RGB}{195,58,45}
\definecolor{Yellow}{RGB}{229,175,0}
\definecolor{Orange}{RGB}{215,123,35}
\definecolor{Brown}{RGB}{80,52,32}
\definecolor{Dark Brown}{RGB}{84,48,26}
\definecolor{Black}{RGB}{0,0,0}
\definecolor{Dark Grey}{RGB}{161,149,137}
\definecolor{Warm Grey}{RGB}{165,158,148}
\definecolor{Light Grey}{RGB}{202,194,184}
\definecolor{Stone}{RGB}{217,214,204}
\definecolor{White}{RGB}{255,255,255}

\makeatletter

\let\tikz@uninstallcommands@orig=\tikz@uninstallcommands

\def\tikz@uninstallcommands{%
    \tikz@uninstallcommands@orig%
    \let\tikz@atbegin@node=\pgfutil@empty%
        \let\tikz@atend@node=\pgfutil@empty}

\newbox\tikzscalebox
\tikzset{
    scale node text/.style={scale node text to=#1 and #1},
    scale node text to/.style args={#1 and #2}{
        execute at begin node={%
            \setbox\tikzscalebox=\hbox\bgroup%
        },
        execute at end node={%
            \egroup%
            \pgfpicture
            \pgftransformxscale{(\wd\tikzscalebox>0) ? #1/\wd\tikzscalebox : 
            1}%
            \pgftransformyscale{((\ht\tikzscalebox+\dp\tikzscalebox)>0) ? 
            #2/(\ht\tikzscalebox+\dp\tikzscalebox) : 1}%
            \pgftext{\box\tikzscalebox}%
            \endpgfpicture%
        }
    },
    tight fit/.style={inner sep=0pt, outer sep=0pt},
    button/.style={ left color=Black!60, right color=Black!70},
    outset bevel/.style={
        preaction={transform canvas={shift={(0.02,0.02)}},fill=Black!90, 
        draw=Black!90, line width=2pt},
    },
    inset bevel/.style={
        preaction={transform canvas={shift={(-0.02,-0.02)}},fill=Black!90, 
        draw=Black!90, line width=1pt},
    },
    backing bevel/.style={
        preaction={transform canvas={shift={(0.16,0.02)}},fill=Black!90, 
                draw=Black!90, line width=6pt},
        preaction={transform canvas={shift={(0.04,0.01)}},fill=Black!40, 
        draw=Black!40, line width=3pt},
    },
  widget/.style={
      shape=rectangle,
      minimum size=0.7125cm,
      scale node text=0.5cm,
      rounded corners=0.05cm,
      inner sep=0.01cm,
    transform shape,
  },
  icon/.style={
      shape=rectangle,
      minimum size=0.375cm,
      rounded corners=0.05cm,
      inner sep=0.01cm,
    transform shape
  }
}
\renewcommand\familydefault\sfdefault

{\catcode`\%=12\gdef\pcchar{%}}
\edef\day{\directlua{tex.write(os.date("\pcchar A"))}}
\edef\time{\directlua{tex.write(os.date("\pcchar I:\pcchar M"))}}
\edef\timesuffix{\directlua{tex.write(os.date("\pcchar p"))}}
\edef\monthdayyear{\directlua{tex.write(os.date("\pcchar B \pcchar d, \pcchar Y"))}}
\def\location{London}

\begin{document}

\rotatebox{-15}{%
\begin{tikzpicture}[scale=0.75]
%
% Handset
\path [backing bevel, fill=Black!80, rounded 
corners=0.125cm]
    (-2,-3) to [bend right, looseness=0.375] (2,-3)
    -- (2,3.5) to [bend right, looseness=0.375] (-2,3.5) -- cycle;
%
% Ear-thing (?)
\foreach \x in {-2,-1,0,1,2}
    \path [inset bevel, fill=Black]
        (\x/4,3.45) circle [radius=0.05];
%
% Microphone
\path [inset bevel, fill=Black, rounded corners=0.05cm]
    (-0.5,-3) rectangle ++(1,0.125);
%
% Screen
\node [inset bevel,tight fit, fill=Blue!75] 
[fit={(-1.75,-2.5)  (1.75,3.25)}] (screen) {};
%
% Signal strength
\node [icon, text=White, below right=0.25 of screen.north 
west] (signal) {\foreach \x in 
{0.06,0.12,...,0.35}{\vrule width0.05cm height\x cm\hskip0.02cm}};
%
% 3G
\node [icon, right=0.125 of signal, fill=Bright Blue, text=White, 
font=\tiny\bf] (3G) {3G};
%
% Battery
\node [icon, text=white, font=\tiny, below left=0.25 of screen.north east] {
80\%\ \begin{tikzpicture}[scale=0.5, baseline={(0,0.05)}, xscale=-1, rounded 
corners=0.01cm]
\fill [White, even odd rule](0,0) |- (-0.1,0.1) |- (0,0.4) |- (1,0.5) |- (0,0) 
-- 
cycle
    (0.05,0.05) rectangle (0.95,0.45)
    (0.25,0.1) rectangle (0.9,0.4);
\end{tikzpicture}   
};
%
% Date and time
\node [tight fit, rounded corners=0.125cm, fill=Black, opacity=0.25, 
fit={(-1.5,2.5) (1.5,-0.5)}] (info 
panel){};
\node [inner sep=0.01cm, text=White, below right=0.25 
of 
info panel.north west, align=left, font=\scriptsize]  
{{\large\time\tiny\timesuffix}\\\day\\\monthdayyear};
%
% Weather, location, temperature
\begin{scope}[shift={(info panel.south east)}, shift=(120:1)]
\fill [White]  circle [radius=0.125];
\foreach \i in {0,45,...,315}
    \draw [White, thick, line cap=round]
        (\i:0.175) -- (\i:0.25);
\end{scope}
\node [text=White, inner sep=0.01cm, text=White, above left=0.25 
of 
info panel.south east, align=left,] {{\scriptsize\location}\ 
\footnotesize18$^\circ$C};
%
% Widgets
\node [widget, fill=Mid Green, above right=0.125 of screen.south west] 
(call)
{\tikz[rounded corners=0, 
rotate=-45]\fill[fill=White] 
(-0.75,-1) 
to [bend right] (-0.25,-1)
to [bend right] (-0.375,-0.75) to [bend left, looseness=0.375]
(0.375,-0.75) to [bend right] (0.25,-1) to [bend right](0.75,-1)
to [bend right] (0.625,-0.75) to [bend right, looseness=0.625] (-0.625,-0.75)
to [bend right] (-0.75,-1) -- cycle;};
%
\node [widget, fill=Orange, right=0.125 of call] (contacts) 
{\tikz[rounded corners=0]{
\useasboundingbox (-1,-1) rectangle (1,1);
\fill[fill=White] (0,0.5) circle[radius=0.5];
\fill[fill=White] (-0.75,-0.75) arc (180:0:0.75 and 0.875) -- cycle; }};
%
\node [widget, fill=Pink, above=0.125 of call] (email) 
{\tikz[rounded corners=0]{
\useasboundingbox (-1,-1) rectangle (1,1);
\fill [White](-1,0.75) -- (1,0.75) -- (0,0) -- cycle;
\fill [White, shift=(270:0.125)](-1,0.75) --  (0,0) -- (1,0.75) |- (-1,-0.625) 
-- cycle;
}};
%
\node [widget, fill=Yellow, right=0.125 of email] (music) 
{\tikz[rounded corners=0]{
\useasboundingbox (-1,-1) rectangle (1,1);
\tikzset{rotate around={30:(0,-0.75)}}
\fill [White](0,-0.75) ellipse [x radius=0.5, y radius=0.375];
\fill [White](0.5,-0.75) rectangle (0.25,0.75);
\fill [White, yslant=-0.25] (0.25,1) rectangle (1,0.625);
}};
%
\node [widget, fill=Navy Blue, right=0.125 of music] (camera) 
{\tikz[rounded corners=0]{
\useasboundingbox (-1,-1) rectangle (1,1);
\fill[White, even odd rule](-1,-0.75) |- (-0.5,0.75) |- (0.25,1) |- 
(1,0.75) |- 
(-1,-0.75) -- cycle
    (-0.25,0) circle [radius=0.625]
    (-0.25,0) circle [radius=0.5]
    (-0.25,0) circle [radius=0.375]
    (0.875,0.625) rectangle (0.375,0.375);
}};
%
\node [widget, fill=Brown, right=0.125 of camera] (camera) 
{\tikz[rounded corners=0]{
\useasboundingbox (-1,-1) rectangle (1,1);
\fill[White, even odd rule] (-1,-0.5) |- (1,1) |- (-0.125,-0.5) --
(-0.375,-1) -- (-0.375,-0.5) -- cycle
(-0.5,0.5) circle [radius=0.175] (0.5,0.5) circle [radius=0.175]
[shift=(90:1), xscale=1.5, yscale=2]
(300:0.5) -- (300:0.6) arc(300:240:0.6) --(240:0.5) arc(240:300:0.5) 
--cycle;
}};
%
\node [widget, fill=Mid Red, scale node text=0.5cm, right=0.125 of contacts]  
(settings)
{\tikz[rounded corners=0] {
\fill[fill=White] (0,0) circle[radius=0.625];
\foreach \x in {0,45,...,315}{
    \fill [White] (\x-10:0.755) -- (\x+10:0.755) -- (\x+20:0.45) -- 
    (\x-20:0.45)
    -- cycle;
}
\fill[fill=Mid Red] (0,0) circle[radius=0.5];}};
%
\node [widget, fill=Dark Purple, scale node text=0.5cm, right=0.125 of 
settings] 
{\tikz[rounded corners=0]{
\foreach \i in {1,...,4}
    \foreach \j in {1,...,4}
        \fill [White] (\i, \j) rectangle ++(0.5,0.5);}};
%
\end{tikzpicture}%
}
\end{document}

enter image description here

share|improve this answer
    
Thank you for these very good pictures. The first code compiles but with the following error message: ! Undefined control sequence. \tikz@pic@code ->\code ={ \begin {scope}[line join=round,looseness=0.25, lin... l.74 \path (0,0) pic {mobile phone} ; I don't know why? The second code compiles error-free though. –  AboAmmar Aug 12 at 23:15
    
I suspect you need to update the PGF distribution. The first picture uses the pic syntax which came along with the last PGF release. The second (older) picture was created before pics were available and just uses a standard tikzpicture inside nodes (which isn't normally advisable). –  Mark Wibrow Aug 13 at 9:26

For re-using, you can use pic:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing,}
\tikzset{radiation/.style={{decorate,decoration={expanding waves,angle=90,segment length=4pt}}},
         antenna/.pic={
        code={\tikzset{scale=5/10}
            \draw[semithick] (0,0) -- (1,4);% left line
            \draw[semithick] (3,0) -- (2,4);% right line
            \draw[semithick] (0,0) arc (180:0:1.5 and -0.5);
            \node[inner sep=4pt] (circ) at (1.5,5.5) {};
            \draw[semithick] (1.5,5.5) circle(8pt);
            \draw[semithick] (1.5,5.5cm-8pt) -- (1.5,4);
            \draw[semithick] (1.5,4) ellipse (0.5 and 0.166);
            \draw[semithick,radiation,decoration={angle=45}] (1.5cm+8pt,5.5) -- +(0:2);
            \draw[semithick,radiation,decoration={angle=45}] (1.5cm-8pt,5.5) -- +(180:2);
  }}
}

\begin{document}
\begin{tikzpicture}
    \path (0,0) pic {antenna};
    \path (4,0) pic[scale=0.75,color=red] {antenna};
    \path (0,-4) pic[scale=0.5,rotate=45] {antenna};
\end{tikzpicture}

\end{document}

enter image description here

I have also defined a new radiation style using exapnding waves decoration.

share|improve this answer
    
And for mobile phone, it is better to use a picture (jpeg or png) inside a node. –  Harish Kumar Aug 12 at 4:54

If you're using the whole figure without changing anything, you could either use and \input{my-tikz-file},

%---- my-tikz-file
\draw[semithick] (0,0) -- (1,4);% left line
\draw[semithick] (3,0) -- (2,4);% right line
\draw[semithick] (0,0) arc (180:0:1.5 and -0.5);
\draw[semithick] (1.5,4) ellipse (0.5 and 0.166);

or define a command (without arguments) with the code for the figure,

\newcommand\mytikzfig{%
  \draw[semithick] (0,0) -- (1,4);% left line
  \draw[semithick] (3,0) -- (2,4);% right line
  \draw[semithick] (0,0) arc (180:0:1.5 and -0.5);
  \draw[semithick] (1.5,4) ellipse (0.5 and 0.166);}
share|improve this answer

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.