Tikz:如何创建和重复使用图片

Tikz:如何创建和重复使用图片

我是初学者Tikz,有两个问题:

  1. 我正在尝试绘制下面的图片,但没有成功(我画出了右边的部分,但画不出左边的部分)
  2. 如果我经常使用这两个形状,我该如何在其他图片中重复使用它们?

这是我用来绘制右侧部分的方法:

\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}

在此处输入图片描述

我很感谢您的帮助,并提前谢谢您。

答案1

正如 Harish Kumar 指出的,“pics”对于重复使用小图片很有用。下面的图片经过了一些尝试,但看起来有点像 OP 所需的图片。

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

\tikzset{mobile phone/.pic={
code={%in this line should be used "code" and not "\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}

在此处输入图片描述

这是我去年为演示文稿创建的(有点过度的)手机图像。由于它在几张幻灯片上重复使用,所以我只需使用该类对其进行编译,standalone每当phone.pdf我需要它时,我都会使用它:

\includegraphics{phone.pdf}

以下内容需要lualatex编译,因为我曾经os.date更新过文档编译时的日期和时间。

\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}

在此处输入图片描述

答案2

对于重复使用,您可以使用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}

在此处输入图片描述

我还使用装饰定义了一种新radiation风格exapnding waves

答案3

如果你要使用整个图形而不做任何改变,你可以使用和\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);

或者用图形代码定义一个命令(不带参数),

\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);}

相关内容