在图形中包含 tikz 时出现奇怪的错误

在图形中包含 tikz 时出现奇怪的错误

我正在尝试将 tikzpicture 包含在图形中。它对其他 tikzpicture 有效,但现在我有一个复杂的 3D 图片这里,我很难融入。

这里是您可以在下方运行并查看错误的 MWE(抱歉,这是我能找到的唯一显示错误的方法)

错误非常普通,像往常一样难以理解,例如“未定义的控制序列”。我不明白,为什么 tikzpicture 作为外部工作正常,但当放入图形内部时却突然不行。正如您所看到的,我需要在图形内部浮动,否则您会发现它的位置不正确。

正面代号:

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}


\usepackage{mytikz_custom}
\pgfplotsset{compat=1.15}
\author{Parthiban Rajendran}
\begin{document}


    % example 3D sample set
\pgfplotstableread{
    X Y Z m
    2.2 14 0 0
    2.7 23 0 0
    3 13 0 0
    3.55 22 0 0
    4 15 0 0
    4.5 20 0 0
    4.75 28 0 0
    5.5 23 0 0
}\datatablet


\section{Runs fine if not inside figure}    
        \begin{tikzpicture}[scale=1.5]
\begin{axis}
[ set layers,   
view={130}{50},
samples=200,
samples y=0, 
xmin=1,xmax=6, ymin=5,ymax=40, zmin=0, zmax=10,
% ytick=\empty,xtick=\empty,ztick=\empty,
clip=false, axis lines = middle,
area plot/.style=   % for this: https://tex.stackexchange.com/questions/53794/plotting-several-2d-functions-in-a-3d-graph
{
    fill opacity=0.5,
    draw=none,
    fill=orange,
    mark=none,
    smooth
}
]               
\GetLocalFrame              
\begin{scope}[transform shape]
\addplot3[only marks, fill=cyan,mark=fcirc] table {\datatablet};
\end{scope}         
\end{axis}
\end{tikzpicture}   


\section{Silly blocking error if put inside figure} 

The error says undefined control sequence. Commented to go through. Same tikz content as above, only now inside figure. 

\begin{figure}
        \begin{tikzpicture}[scale=1.5]
\begin{axis}
[ set layers,   
view={130}{50},
samples=200,
samples y=0, 
xmin=1,xmax=6, ymin=5,ymax=40, zmin=0, zmax=10,
% ytick=\empty,xtick=\empty,ztick=\empty,
clip=false, axis lines = middle,
area plot/.style=   % for this: https://tex.stackexchange.com/questions/53794/plotting-several-2d-functions-in-a-3d-graph
{
    fill opacity=0.5,
    draw=none,
    fill=orange,
    mark=none,
    smooth
}
]               
\GetLocalFrame              
\begin{scope}[transform shape]
\addplot3[only marks, fill=cyan,mark=fcirc] table {\datatablet};
\end{scope}         
\end{axis}
\end{tikzpicture}   
\end{figure}

\end{document}

这里是风格包mytikz_custom

答案1

因为我搞砸了,所以这里有一个更干净的解决方案。只需将其用于样式文件即可:

% This style package was created using hint from here: https://tex.stackexchange.com/questions/77/how-to-make-a-standard-preamble-into-a-package?rq=1

% Declare that this style file requires at least LaTeX version 2e.
\NeedsTeXFormat{LaTeX2e}

% Provide the name of your page, the date it was last updated, and a comment about what it's used for
\ProvidesPackage{mytikz_custom}[2018/09/06 Hardcoded preamble to be used in main tex to support subfiles (sub-files should include and refer here the preamble they need)]

\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{3d,calc,math, decorations.pathreplacing,angles,quotes,bending, arrows.meta}


% small fix for canvas is xy plane at z % https://tex.stackexchange.com/a/48776/121799
\makeatletter
\tikzoption{canvas is xy plane at z}[]{%
    \def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
    \def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
    \def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
    \tikz@canvas@is@plane}
\makeatother

%\pgfplotsset{compat=1.15}


% ref: https://tex.stackexchange.com/questions/456138/marks-do-not-appear-in-3d-for-3d-scatter-plot/456142
\pgfdeclareplotmark{fcirc}
{%          
    \begin{scope}[expand style={local frame}{\MyLocalFrame},local frame]
        \begin{scope}[canvas is xy plane at z=0,transform shape]
            \fill circle(0.1);
        \end{scope}   
    \end{scope}
}% based on https://tex.stackexchange.com/a/64237/121799
\tikzset{expand style/.code n args={2}{\tikzset{#1/.style/.expanded={#2}}}}
\newcommand{\GetLocalFrame}
{
    \path let \p1=(     $(1,0,0)-(0,0,0)$   ), \p2=(    $(0,1,0)-(0,0,0)$   ), \p3=(   $(0,0,1)-(0,0,0)$   )  % these look like axes line paths
    in \pgfextra  %pgfextra is to execute below code before constructing the above path 
    {
        \pgfmathsetmacro{\ratio}
        {   
            veclen(\x1,\y1)/veclen(\x2,\y2)  
        }
        \xdef\MyLocalFrame{   
                x   =  {   (\x1,\y1)    },
                y   =  {    (\ratio*\x2,\ratio*\y2)     },
                z   =   {     (\x3,\y3)     }
            }
    }; 
}

\tikzset
{
    declare function={
        % normal(\m,\s)=1/(2*\s*sqrt(pi))*exp(-(x-\m)^2/(2*\s^2));
        normal(\x,\m,\s) = 1/(2*\s*sqrt(pi))*exp(-(\x-\m)^2/(2*\s^2));
    }
}

% Finally, we'll use \endinput to indicate that LaTeX can stop reading this file. LaTeX will ignore anything after this line.
\endinput

然后它就起作用了。 我也会清理我之前的答案。

相关内容