如何将 \begin{scope} Tikz 与 `tikz-3dplot` 以及另一个普通的 `tikz` 结合起来

如何将 \begin{scope} Tikz 与 `tikz-3dplot` 以及另一个普通的 `tikz` 结合起来

它只是将两张图片组合在一起,一个使用tikz-3dplot另一个只是tikz。这是代码。我希望图片左右排列。我通常使用\begin{scope}..\end{scope但这次的代码完全不同,一个是 3D,另一个只是 2D。

3D 代码:

\documentclass[border=2mm,12pt,tikz]{standalone}
\usepackage{tikz-3dplot} 
\usetikzlibrary{3d,calc,backgrounds}
\usepackage{fouriernc}
%% smuggling from https://tex.stackexchange.com/a/470979/121799
\newcounter{smuggle}
\DeclareRobustCommand\smuggleone[1]{%
    \stepcounter{smuggle}%
    \expandafter\global\expandafter\let\csname smuggle@\arabic{smuggle}\endcsname#1%
    \aftergroup\let\aftergroup#1\expandafter\aftergroup\csname smuggle@\arabic{smuggle}\endcsname
}
\DeclareRobustCommand\smuggle[2][1]{%
    \smuggleone{#2}%
    \ifnum#1>1
    \aftergroup\smuggle\aftergroup[\expandafter\aftergroup\the\numexpr#1-1\aftergroup]\aftergroup#2%
    \fi
}
\begin{document}
    % please do not use \x and \y since these are used in the calc syntax
    \def\myx{1}
    \def\myy{3}
    \def\h{6}
    
    \tdplotsetmaincoords{83}{70}
    \begin{tikzpicture}[tdplot_main_coords]
        %    \draw[-latex] (0,0,0) -- (1,0,0) node[pos=1.1]{$x$};
        %    \draw[-latex] (0,0,0) -- (0,1,0) node[pos=1.1]{$y$};
        %    \draw[-latex] (0,0,0) -- (0,0,1) node[pos=1.1]{$z$};
        \begin{scope}[canvas is xz plane at y=0]
            \draw[thick] (0,0) coordinate[label=above:$$] (O) circle (\myy);
        \end{scope}
        \begin{scope}[canvas is xz plane at y=\h]
            \path (0,0) coordinate[label=above:$$] (O');   
            \draw[thick] let \p1=($(O')-(O)$),\n1={atan2(\y1,\x1)} in
            \pgfextra{\pgfmathsetmacro{\myangle}{\n1}\smuggle{\myangle}} 
            ($(O)+({90+\n1}:\myy)$) --
            ({90+\n1}:\myy) arc({90+\n1}:{-90+\n1}:\myy) --
            ($(O)+({-90+\n1}:\myy)$) ;
            \begin{scope}[on background layer,every label/.append style={black}]
                \draw[dashed] ({90+\myangle}:\myy) arc({90+\myangle}:{180+90+\myangle}:\myy); 
                % front
                \shade[top color=blue!30,bottom color=blue,shading angle=\myangle] 
                ($(O)+({-20}:\myy)$) -- ($(O')+({-20}:\myy)$) 
                arc(-20:-90+\myangle:\myy) --($(O)+({-90+\myangle}:\myy)$)
                arc(-90+\myangle:-20:\myy);
                % left
                \shade let \p1=($({-160}:\myy)-({-20}:\myy)$),\n1={180+atan2(\y1,\x1)} in
                [top color=blue!30,bottom color=blue,shading angle=\n1]
                ($(O)+({-20}:\myy)$) arc(-20:-160:\myy) -- cycle;
                %top 
                \fill[blue!20]  ($(O')+({-20}:\myy)$) coordinate[label=right:$$](B') 
                -- ($(O')+({-160}:\myy)$) coordinate[label=above left:$$](A') 
                -- ($(O)+({-160}:\myy)$) coordinate[label=left:$$](A) 
                -- ($(O)+({-20}:\myy)$) coordinate[label=right:$$](B) 
                -- cycle;
            \end{scope}
        \end{scope}
    \end{tikzpicture}
\end{document} 

二维码:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}
    \begin{tikzpicture}[scale=1,>=latex,x=1cm,y=0.8cm]
        
        \draw (0,0) ellipse (4 and 4);% a circle 
        \draw[fill=blue!7] (-4,0) arc (180:360:4 and 4);% left half of the circle starting at (-4,0) from 180 to 360
        \draw[fill=blue!37] ({sqrt(12)},-2) -- (-{sqrt(12)},-2) -- (-{sqrt(9.75)},-2.5) -- ({sqrt(9.75)},-2.5) -- cycle;
        
        \draw [decorate,decoration={brace,amplitude=8pt},xshift=0.4pt,yshift=0.7pt]
        (0.2,-1.9) -- (3.4,-1.9) node [black,above, midway,xshift=-0.6cm] 
        {$$};
        \draw (2,-1.1) node {$\sqrt{16 - y^{2}}$};
        
        \draw (4.1,4.2) node {$x^{2} + y^{2} = 16$};
        \draw (-0.6,4.3) node {$(0,4)$};
        \draw (4.5,0.3) node {$(4,0)$}; 
        
        \draw[<->] (-4.2,0) -- (-4.2,-2) node[left, midway]  {\footnotesize $-y$};
        \draw[<->] (3.8,-2) -- (3.8,-2.5) node[right, midway]  {\footnotesize $\triangle y$};
        \draw[->,thick] (-5,0) -- (6,0) node[above] {\footnotesize $x$};
        \draw[->,thick] (0,-5) -- (0,6) node[below right]{\footnotesize $y$};
    \end{tikzpicture}
\end{document}

1

答案1

\begin{scope}..\end{scope}工作正常,您只需将tikzpicture选项移动到相应的scope

\documentclass[border=2mm,12pt,tikz]{standalone}
%\usepackage{tikz}
\usepackage{tikz-3dplot}
\usetikzlibrary{3d,calc,backgrounds}
\usetikzlibrary{decorations.pathreplacing}
\usepackage{fouriernc}
%% smuggling from https://tex.stackexchange.com/a/470979/121799
\newcounter{smuggle}
\DeclareRobustCommand\smuggleone[1]{%
    \stepcounter{smuggle}%
    \expandafter\global\expandafter\let\csname smuggle@\arabic{smuggle}\endcsname#1%
    \aftergroup\let\aftergroup#1\expandafter\aftergroup\csname smuggle@\arabic{smuggle}\endcsname
}
\DeclareRobustCommand\smuggle[2][1]{%
    \smuggleone{#2}%
    \ifnum#1>1
    \aftergroup\smuggle\aftergroup[\expandafter\aftergroup\the\numexpr#1-1\aftergroup]\aftergroup#2%
    \fi
}
\begin{document}
    % please do not use \x and \y since these are used in the calc syntax
    \def\myx{1}
    \def\myy{3}
    \def\h{6}
    
    \tdplotsetmaincoords{83}{70}
    \begin{tikzpicture}[]
    \begin{scope}[tdplot_main_coords]
        %    \draw[-latex] (0,0,0) -- (1,0,0) node[pos=1.1]{$x$};
        %    \draw[-latex] (0,0,0) -- (0,1,0) node[pos=1.1]{$y$};
        %    \draw[-latex] (0,0,0) -- (0,0,1) node[pos=1.1]{$z$};
        \begin{scope}[canvas is xz plane at y=0]
            \draw[thick] (0,0) coordinate[label=above:$$] (O) circle (\myy);
        \end{scope}
        \begin{scope}[canvas is xz plane at y=\h]
            \path (0,0) coordinate[label=above:$$] (O');   
            \draw[thick] let \p1=($(O')-(O)$),\n1={atan2(\y1,\x1)} in
            \pgfextra{\pgfmathsetmacro{\myangle}{\n1}\smuggle{\myangle}} 
            ($(O)+({90+\n1}:\myy)$) --
            ({90+\n1}:\myy) arc({90+\n1}:{-90+\n1}:\myy) --
            ($(O)+({-90+\n1}:\myy)$) ;
            \begin{scope}[on background layer,every label/.append style={black}]
                \draw[dashed] ({90+\myangle}:\myy) arc({90+\myangle}:{180+90+\myangle}:\myy); 
                % front
                \shade[top color=blue!30,bottom color=blue,shading angle=\myangle] 
                ($(O)+({-20}:\myy)$) -- ($(O')+({-20}:\myy)$) 
                arc(-20:-90+\myangle:\myy) --($(O)+({-90+\myangle}:\myy)$)
                arc(-90+\myangle:-20:\myy);
                % left
                \shade let \p1=($({-160}:\myy)-({-20}:\myy)$),\n1={180+atan2(\y1,\x1)} in
                [top color=blue!30,bottom color=blue,shading angle=\n1]
                ($(O)+({-20}:\myy)$) arc(-20:-160:\myy) -- cycle;
                %top 
                \fill[blue!20]  ($(O')+({-20}:\myy)$) coordinate[label=right:$$](B') 
                -- ($(O')+({-160}:\myy)$) coordinate[label=above left:$$](A') 
                -- ($(O)+({-160}:\myy)$) coordinate[label=left:$$](A) 
                -- ($(O)+({-20}:\myy)$) coordinate[label=right:$$](B) 
                -- cycle;
            \end{scope}
        \end{scope}
        \end{scope}
        
        \begin{scope}[scale=1,>=latex,x=1cm,y=0.8cm,xshift=-8cm]
                \draw (0,0) ellipse (4 and 4);% a circle 
            \draw[fill=blue!7] (-4,0) arc (180:360:4 and 4);% left half of the circle starting at (-4,0) from 180 to 360
            \draw[fill=blue!37] ({sqrt(12)},-2) -- (-{sqrt(12)},-2) -- (-{sqrt(9.75)},-2.5) -- ({sqrt(9.75)},-2.5) -- cycle;
            
            \draw [decorate,decoration={brace,amplitude=8pt},xshift=0.4pt,yshift=0.7pt]
            (0.2,-1.9) -- (3.4,-1.9) node [black,above, midway,xshift=-0.6cm] 
            {$$};
            \draw (2,-1.1) node {$\sqrt{16 - y^{2}}$};
            
            \draw (4.1,4.2) node {$x^{2} + y^{2} = 16$};
            \draw (-0.6,4.3) node {$(0,4)$};
            \draw (4.5,0.3) node {$(4,0)$}; 
            
            \draw[<->] (-4.2,0) -- (-4.2,-2) node[left, midway]  {\footnotesize $-y$};
            \draw[<->] (3.8,-2) -- (3.8,-2.5) node[right, midway]  {\footnotesize $\triangle y$};
            \draw[->,thick] (-5,0) -- (6,0) node[above] {\footnotesize $x$};
            \draw[->,thick] (0,-5) -- (0,6) node[below right]{\footnotesize $y$};
        \end{scope}
        
    \end{tikzpicture}
\end{document} 

结果: 在此处输入图片描述

相关内容