如何在 Tikz 中将火柴人变成图片对象?

如何在 Tikz 中将火柴人变成图片对象?

我希望有人能帮助我将这个火柴人代码转换成 Tikz 图片。

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{shapes.callouts} 
%--------------------
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage[utf8]{inputenc} %useful to type directly diacritic characters
\usepackage[siunitx,american, RPvoltages]{circuitikz}  
\usetikzlibrary{positioning} 
\usepackage{pgfplots}
\usetikzlibrary{arrows,shapes,graphs}
\usepackage{physics}
\usepackage{ifthen}
\usepackage{cancel}
\usepackage[portuges]{babel}
%% 
\usepackage[electronic]{ifsym}
\usepackage{xstring}
\usepackage{xcolor}
\usepackage{marvosym} %Smiley
\usepackage{tikzsymbols}%\Cooley
\usepackage{ifthen}
\usepackage{caption}
\pgfplotsset{compat=newest}



%%%%%%%%%%%5
\newcommand{\stickman}[9] % all parameters should be between -1 and 1.
% \stickman{Char}{rightArmpit}{rightElbow}{rightLeg}{rightKnee}  {leftArmpit}{lefttElbow}{leftLeg}{lefttKnee}
{
    \def\diaHead{0.3}
    \def\rap{#2}  \def\relb{#3}   \def\lap{#6}  \def\lelb{#7}
    \def\rleg{#4} \def\rknee{#5}  \def\lleg{#8} \def\lknee{#9} 

    \def\phead{(180-90*\rap+90*\lap)/2-90}

    \draw[-{Ellipse[line cap=round,length=1.25mm]},thick] (0,0) to[short] ++(-90+180*\lap:2*\diaHead) to ++(180*\lelb+180*\lap-90:2*\diaHead);
    \draw[-{Ellipse[line cap=round,length=1.25mm]},thick] (0,0) to[short] ++(270-180*\rap:2*\diaHead) to ++(270-180*\relb-180*\rap:2*\diaHead);

    \draw[line width=1.25pt, line cap=mitter] (0,0) to ++(0,-3*\diaHead) node[inner sep=0pt](v){};
    \draw[-Bar,thick] (v) to[short] ++(-85+170*\lleg:2*\diaHead) to[short] ++(-85+170*\lknee+170*\lleg:2*\diaHead);
    \draw[-Bar,thick] (v) to[short] ++(-95-170*\rleg:2*\diaHead) to[short] ++(-95-170*\rknee-170*\rleg:2*\diaHead);

    \node[rotate=\phead, scale=1] at (0,0){\rotatebox[origin=c]{0}{\raisebox{1.0\height}{#1}} } ;
}


\begin{document}


    \begin{tikzpicture}[]
    \draw[step=2mm] [help lines, black!10]  (0,-5) grid (10,5);
    \draw[step=10mm] [help lines, blue!30]  (0,-5) grid (10,5);

    \begin{scope}[xshift=4cm,yshift=4cm,xscale=1]
    \stickman{\Smiley{}}{0.1}{0.5}{-0.2}{0.1}  {0.1}{0.5}{0}{0}
    \end{scope}

    \begin{scope}[xshift=2cm,yshift=4cm,scale=0.3]
    \stickman{\Smiley{}}{0.5}{0.15}{0}{0}  {0.45}{0.75}{0}{0}
    \end{scope}

    \begin{scope}[xshift=4cm,yshift=0.5cm,xscale=1, color=gray]
    \stickman{\Smiley{}}{0.1}{0}{0}{0}  {0.1}{0.5}{0}{0}
    \end{scope}

    \begin{scope}[xshift=5cm,yshift=0.4cm,xscale=1, color=gray]
    \stickman{\Smiley{}}{0.25}{-0.4}{0}{0.25}  {0.1}{0.5}{0.25}{-0.5}
    \end{scope}

    \begin{scope}[xshift=6cm,yshift=0.1cm,xscale=1]
    \stickman{\Tongey{}}{0.25}{-0.4}{0}{0.5}  {0.4}{0.15}{0.5}{-0.5}
    \end{scope}

    \draw[ thick] (7.5,-2) node[ground](){} to[short]++(0,1) to[nopb] ++(0,2) to[cute inductor]++(0,2) node[vcc](){$+V_{CC}$};

    \end{tikzpicture} 


\end{document} 

答案1

这会将您的宏变成图片。除了将文档的前言压缩为一组更精简的软件包外,主要的挑战是猜测宏名称代表什么,我很确定我没有全部猜对,但没有太多的解释。不过,我确实将每个宏都映射到一个键上。您现在可以说

\path (5,0) pic{stickman={right arm=0.1,left knee=0.2,head=\Tongey}};

其中未指定的键采用一些默认/初始值,您可以通过添加更多键来升级您的图片,而不会失去向后兼容性。

\documentclass[11pt]{scrartcl}
\usepackage[siunitx,american, RPvoltages]{circuitikz} 
\usetikzlibrary{shapes.callouts} 
%--------------------
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage{marvosym} %Smiley
\usepackage{tikzsymbols}%\Cooley



\tikzset{pics/stickman/.style={code={
    \tikzset{stickman/.cd,#1}
    \def\pv##1{\pgfkeysvalueof{/tikz/stickman/##1}}%
    %
    \def\phead{(180-90*\pv{right arm}+90*\pv{left arm})/2-90}
    %
    \draw[-{Ellipse[line cap=round,length=1.25mm]},thick] (0,0) to[short] ++(-90+180*\pv{left arm}:2*\pv{head diameter}) to ++(180*\pv{left ellbow}+180*\pv{left arm}-90:2*\pv{head diameter});
    \draw[-{Ellipse[line cap=round,length=1.25mm]},thick] (0,0) to[short] ++(270-180*\pv{right arm}:2*\pv{head diameter}) to ++(270-180*\pv{right ellbow}-180*\pv{right arm}:2*\pv{head diameter});
    %
    \draw[line width=1.25pt, line cap=mitter] (0,0) to ++(0,-3*\pv{head diameter}) node[inner sep=0pt](v){};
    \draw[-Bar,thick] (v) to[short] ++(-85+170*\pv{left leg}:2*\pv{head diameter}) to[short] ++(-85+170*\pv{left knee}+170*\pv{left leg}:2*\pv{head diameter});
    \draw[-Bar,thick] (v) to[short] ++(-95-170*\pv{right leg}:2*\pv{head diameter}) to[short] ++(-95-170*\pv{right knee}-170*\pv{right leg}:2*\pv{head diameter});
    %
    \node[rotate=\phead, scale=1] at (0,\pv{head diameter}/2)   {\pv{head}} ;
    }},stickman/.cd,head diameter/.initial=0.3,head/.initial=\Smiley,
    right arm/.initial=0.25,left arm/.initial=0.25,
    right ellbow/.initial=0.25,left ellbow/.initial=0.25,
    right leg/.initial=0,left leg/.initial=0,
    right knee/.initial=0,left knee/.initial=0}

\begin{document}


\begin{tikzpicture}[]
    \draw[step=2mm] [help lines, black!10]  (0,-5) grid (10,5);
    \draw[step=10mm] [help lines, blue!30]  (0,-5) grid (10,5);

    \path (2,0) pic{stickman} 
    (5,0) pic{stickman={right arm=0.1,left knee=0.2,head=\Tongey}};

    \draw[ thick] (7.5,-2) node[ground](){} to[short]++(0,1) to[nopb] ++(0,2) to[cute inductor]++(0,2) node[vcc](){$+V_{CC}$};

\end{tikzpicture} 
\end{document} 

在此处输入图片描述

Rmano 的一个版本。

\documentclass[11pt]{scrartcl}
\usepackage[siunitx,american, RPvoltages]{circuitikz} 
\usetikzlibrary{shapes.callouts} 
%--------------------
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage{marvosym} %Smiley
\usepackage{tikzsymbols}%\Cooley
\makeatletter
\tikzset{use fpu reciprocal/.code={%
\def\pgfmathreciprocal@##1{%
    \begingroup
    \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}%
    \pgfmathparse{1/##1}%
    \pgfmath@smuggleone\pgfmathresult
    \endgroup
}}}%
\makeatother

\usetikzlibrary{decorations.pathmorphing,fpu}

\tikzset{pics/stickman/.style={code={
    \tikzset{stickman/.cd,#1}
    \def\pv##1{\pgfkeysvalueof{/tikz/stickman/##1}}%
    %
    \def\phead{(180-90*\pv{right arm}+90*\pv{left arm})/2-90}
    %
    \draw[/tikz/stickman/body,-{Ellipse[line cap=round,length=1.25mm]},thick] (0,0) to[short] ++(-90+180*\pv{left arm}:2*\pv{head diameter}) to ++(180*\pv{left ellbow}+180*\pv{left arm}-90:2*\pv{head diameter});
    \draw[/tikz/stickman/body,-{Ellipse[line cap=round,length=1.25mm]},thick] (0,0) to[short] ++(270-180*\pv{right arm}:2*\pv{head diameter}) to ++(270-180*\pv{right ellbow}-180*\pv{right arm}:2*\pv{head diameter});
    %
    \draw[/tikz/stickman/body,line width=1.25pt, line cap=mitter] (0,0) to ++(0,-3*\pv{head diameter}) node[inner sep=0pt](v){};
    \draw[/tikz/stickman/body,-Bar,thick] (v) to[short] ++(-85+170*\pv{left leg}:2*\pv{head diameter}) to[short] ++(-85+170*\pv{left knee}+170*\pv{left leg}:2*\pv{head diameter});
    \draw[/tikz/stickman/body,-Bar,thick] (v) to[short] ++(-95-170*\pv{right leg}:2*\pv{head diameter}) to[short] ++(-95-170*\pv{right knee}-170*\pv{right leg}:2*\pv{head diameter});
    %
    \node[rotate=\phead, scale=1,decorate=false] at (0,\pv{head diameter}/2)   {\pv{head}} ;
    }},stickman/.cd,head diameter/.initial=0.3,head/.initial=\Smiley,
    right arm/.initial=0.25,left arm/.initial=0.25,
    right ellbow/.initial=0.25,left ellbow/.initial=0.25,
    right leg/.initial=0,left leg/.initial=0,
    right knee/.initial=0,left knee/.initial=0,
    body/.style={}}

\begin{document}


\begin{tikzpicture}[]
    \path[use fpu reciprocal] 
    (5,0) pic[]{stickman={right arm=0.1,left knee=0.2,head=\Tongey,
    body/.style={decoration={random steps,segment
    length=1mm,amplitude=1.2pt},decorate}}};

\end{tikzpicture} 
\end{document} 

在此处输入图片描述

(这实际上说明了一点:我们可以随时升级这些图片,而旧的语法将继续起作用并产生原始结果。)

答案2

修正了“ellbow”的拼写,添加了袜子+鞋子(以防需要隔离),并测试了电击棍子人,只是为了好玩!

\documentclass[11pt]{scrartcl}
\usepackage[siunitx,american, RPvoltages]{circuitikz} 
\usetikzlibrary{shapes.callouts} 
%--------------------
\usepackage{amssymb} %maths
\usepackage{amsmath} %maths
\usepackage{physics}
\usepackage{marvosym} %Smiley
\usepackage{tikzsymbols}%\Cooley
\makeatletter
\tikzset{use fpu reciprocal/.code={%
\def\pgfmathreciprocal@##1{%
    \begingroup
    \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}%
    \pgfmathparse{1/##1}%
    \pgfmath@smuggleone\pgfmathresult
    \endgroup
}}}%
\makeatother

\usetikzlibrary{decorations.pathmorphing,fpu}

\tikzset{pics/stickman/.style={code={
    \tikzset{stickman/.cd,#1}
    \def\pv##1{\pgfkeysvalueof{/tikz/stickman/##1}}%
    %
    \def\phead{(180-90*\pv{right arm}+90*\pv{left arm})/2-90}
    %
    \draw[/tikz/stickman/body,-{Ellipse[line cap=round,length=1.25mm]},thick] (0,0) to[short] ++(-90+180*\pv{left arm}:2*\pv{head diameter}) to ++(180*\pv{left elbow}+180*\pv{left arm}-90:2*\pv{head diameter});
    \draw[/tikz/stickman/body,-{Ellipse[line cap=round,length=1.25mm]},thick] (0,0) to[short] ++(270-180*\pv{right arm}:2*\pv{head diameter}) to ++(270-180*\pv{right elbow}-180*\pv{right arm}:2*\pv{head diameter});
    %
    \draw[/tikz/stickman/body,line width=1.25pt, line cap=mitter] (0,0) to ++(0,-3*\pv{head diameter}) node[inner sep=0pt](v){};
    \draw[/tikz/stickman/body,-\pv{shoes},thick] (v) to[short] ++(-85+170*\pv{left leg}:2*\pv{head diameter}) to[short] ++(-85+170*\pv{left knee}+170*\pv{left leg}:2*\pv{head diameter});
    \draw[/tikz/stickman/body,-\pv{shoes},thick] (v) to[short] ++(-95-170*\pv{right leg}:2*\pv{head diameter}) to[short] ++(-95-170*\pv{right knee}-170*\pv{right leg}:2*\pv{head diameter});
    %
    \node[rotate=\phead, scale=1,decorate=false] at (0,\pv{head diameter}/2)   {\pv{head}} ;
    }},stickman/.cd,
    head diameter/.initial=0.3,
    head/.initial=\Smiley,
    right arm/.initial=0.25,
    left arm/.initial=0.25,
    right elbow/.initial=0.25,
    left elbow/.initial=0.25,
    right leg/.initial=0,
    left leg/.initial=0,
    right knee/.initial=0,
    left knee/.initial=0,
    shoes/.initial={|},
    body/.style={}}

\begin{document}


\begin{circuitikz}[ note/.style={rectangle callout, fill=#1}]
    \draw (0,-2.1) --++(8,0); % a ground floor
    % safe to touch
    \begin{scope}
        \draw[] (2,-1.6) node[cground](){} to[ncpb] ++(0,2) to[cute inductor, v>=$V_s$, i<=$i$]++(0,2) node[vcc](){$V_{s}$};
        \path[] 
        (0.7,0) pic[]{stickman={right arm=0.1,right elbow=-0.4,right knee=0.5,
                left arm=0.25,left elbow=0.15,left leg=0.4,left knee=-0.5,
                shoes={Latex[reversed,color=lightgray]Bar[color=orange]Bar[color=black]},
                head=\Smiley}}
        (3,0) pic[]{stickman={right arm=0.55,right elbow=0.15,left knee=0.2,head=\Tongey[][][red]}}
        ;
        \node [note=red!20, callout relative pointer={(0.4,-0.5)},rounded corners, text width=1.5cm] at (0,1.25) {Shall I open it?};
        \node [note=blue!20, callout relative pointer={(-0.4,-0.5)},rounded corners] at (3.5,1) {Is it safe?};
    \end{scope}

    % It's not always safe to touch 
    \begin{scope}[xshift=4cm]
        \draw[] (2,-1.6) node[cground](){} to[nopb] ++(0,2) to[cute inductor,v={\,}, a= \color{red}{$\Delta V{=}L\dv{i}{t}$}, i<=$i$]++(0,2) node[vcc](){$V_{s}$};

        % safe to touch
        \path[] 
        (0.8,0) pic[]{stickman={right arm=0.1,right elbow=-0.4,right knee=0.5,
                left arm=0.15,left elbow=0.3,left leg=0.5,left knee=-0.5,
                shoes={Latex[reversed,color=lightgray]Bar[color=orange]Bar[color=black]},
                head=\Sadey}}
        ;
        \path[use fpu reciprocal, orange] 
        (3,0) pic[]{stickman={right arm=0.55,right elbow=0.15,right knee=-0.2,left elbow=0.75,left leg=0.15,left knee=0.5,head=\NiceReapey,
                body/.style={decoration={random steps,segment
                length=1mm,amplitude=1.2pt},decorate}}};

        \node [note=red!20, callout relative pointer={(-0.3,-0.5)},rounded corners] at (1.25,1) {No!};
    \end{scope}

\end{circuitikz} 

\end{document}

在此处输入图片描述

相关内容