使用带有 caption 包的 tikz 时出现多个错误

使用带有 caption 包的 tikz 时出现多个错误

我正在尝试使用https://github.com/HarisIqbal88/PlotNeuralNet使用 tikz 创建绘图。我使用 Overleaf 和 LuaLaTeX 进行编译。

仅当我使用我需要的 \usepackage{caption} 时,才会出现以下错误:

  1. 包标题错误:\caption 超出浮点数。

  2. @gobbletwo 的参数有一个额外的}。

  3. 失控的争论?

我的代码:

\documentclass[chapterprefix=false, 12pt, a4paper, oneside, parskip=half, listof=totoc, bibliography=totoc, numbers=noendperiod]{scrbook}

\usepackage{caption}   % without this, there are no errors.

\usepackage{tikz}
\usepackage{import}
\subimport{./layers/}{init}
\usetikzlibrary{positioning}
\usetikzlibrary{3d}
\usetikzlibrary{patterns}

% Solved "Class scrbook Error: undefined old font command `\bf'." from scrbook (?)
\DeclareOldFontCommand{\bf}{\normalfont\bfseries}{\mathbf}

\begin{document}
\begin{tikzpicture}

\def\ConvColorR{rgb:red,1;white,5}
\def\ConvColorG{rgb:green,1;white,5}
\def\ConvColorB{rgb:blue,1;white,5}

\foreach \sy/\col in {
    0/\ConvColorR, 
    1/\ConvColorG, 
    2/\ConvColorB
    } {   
    \pic[shift={(\sy * 0.6, 0, 0)}] at (-8, 0,0) {Box={fill=\col,opacity=0.7,height=10,width=1,depth=10}};
}
% Above line is where the errors are located at

\end{tikzpicture}
\end{document}

我尝试改变 \usepackage{tikz} 和 \usepackage{caption} 之间的顺序

在 foreach 块的右括号后添加 % 会导致不同的错误:

  1. 包标题错误:\caption 超出浮点数。
  2. 失控的争论?
  3. 失控的争论?

答案1

欢迎来到 TeX.SX!

让我们看一下该文件Box.sty

\ProvidesPackage{Box}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This Block can draw simple block of boxes with custom colors. 
% Can be used for conv, deconv etc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tikzset{Box/.pic={\tikzset{/boxblock/.cd,#1}
        \tikzstyle{box}=[every edge/.append style={pic actions, densely dashed, opacity=.7},fill opacity=\opacity, pic actions,fill=\fill]
        
        \pgfmathsetmacro{\y}{\cubey*\scale}
        \pgfmathsetmacro{\z}{\cubez*\scale}
   
        %Multiple concatenated boxes
        \foreach[count=\i,%
                 evaluate=\i as \xlabel using {array({\boxlabels},\i-1)},% 
                 evaluate=\unscaledx as \k using {\unscaledx*\scale+\prev}, remember=\k as \prev (initially 0)] 
                 \unscaledx in \cubex
        {
            \pgfmathsetmacro{\x}{\unscaledx*\scale}
            \coordinate (a) at (\k-\x , \y/2 , \z/2); 
            \coordinate (b) at (\k-\x ,-\y/2 , \z/2); 
            \coordinate (c) at (\k    ,-\y/2 , \z/2); 
            \coordinate (d) at (\k    , \y/2 , \z/2); 
            \coordinate (e) at (\k    , \y/2 ,-\z/2); 
            \coordinate (f) at (\k    ,-\y/2 ,-\z/2); 
            \coordinate (g) at (\k-\x ,-\y/2 ,-\z/2); 
            \coordinate (h) at (\k-\x , \y/2 ,-\z/2); 
        
            \draw [box] 
                (d) -- (a) -- (b) -- (c) -- cycle     
                (d) -- (a) -- (h) -- (e) -- cycle
                %dotted edges
                (f) edge (g)
                (b) edge (g)
                (h) edge (g)    
            ;
            \path (b) edge ["\xlabel"',midway] (c);
            
            \xdef\LastEastx{\k} %\k persists as \LastEastx after loop 
        }%Loop ends
        \draw [box] (d) -- (e) -- (f) -- (c) -- cycle; %East face of last box     
        
        \coordinate (a1) at (0 , \y/2 , \z/2);
        \coordinate (b1) at (0 ,-\y/2 , \z/2);
        \tikzstyle{depthlabel}=[pos=0,text width=14*\z,text centered,sloped]       
        
        \path (c) edge ["\small\zlabel"',depthlabel](f); %depth label
        \path (b1) edge ["\ylabel",midway] (a1);  %height label
        
        
        \tikzstyle{captionlabel}=[text width=15*\LastEastx/\scale,text centered]       
        \path (\LastEastx/2,-\y/2,+\z/2) + (0,-25pt) coordinate (cap) 
        edge ["\textcolor{black}{ \bf \caption}"',captionlabel](cap) ; %Block caption/pic object label
         
        %Define nodes to be used outside on the pic object
        \coordinate (\name-west)   at (0,0,0) ;
        \coordinate (\name-east)   at (\LastEastx, 0,0) ;
        \coordinate (\name-north)  at (\LastEastx/2,\y/2,0);
        \coordinate (\name-south)  at (\LastEastx/2,-\y/2,0);       
        \coordinate (\name-anchor) at (\LastEastx/2, 0,0) ;
        
        \coordinate (\name-near) at (\LastEastx/2,0,\z/2);
        \coordinate (\name-far)  at (\LastEastx/2,0,-\z/2);       
        
        \coordinate (\name-nearwest) at (0,0,\z/2);
        \coordinate (\name-neareast) at (\LastEastx,0,\z/2);
        \coordinate (\name-farwest)  at (0,0,-\z/2);
        \coordinate (\name-fareast)  at (\LastEastx,0,-\z/2);
        
        \coordinate (\name-northeast) at (\name-north-|\name-east);
        \coordinate (\name-northwest) at (\name-north-|\name-west);
        \coordinate (\name-southeast) at (\name-south-|\name-east);
        \coordinate (\name-southwest) at (\name-south-|\name-west);
        
        \coordinate (\name-nearnortheast)  at (\LastEastx, \y/2, \z/2);
        \coordinate (\name-farnortheast)   at (\LastEastx, \y/2,-\z/2);
        \coordinate (\name-nearsoutheast)  at (\LastEastx,-\y/2, \z/2);
        \coordinate (\name-farsoutheast)   at (\LastEastx,-\y/2,-\z/2);
        
        \coordinate (\name-nearnorthwest)  at (0, \y/2, \z/2);
        \coordinate (\name-farnorthwest)   at (0, \y/2,-\z/2);
        \coordinate (\name-nearsouthwest)  at (0,-\y/2, \z/2);
        \coordinate (\name-farsouthwest)   at (0,-\y/2,-\z/2);
        
    },
    /boxblock/.search also={/tikz},
    /boxblock/.cd,
    width/.store        in=\cubex,
    height/.store       in=\cubey,
    depth/.store        in=\cubez,
    scale/.store        in=\scale,
    xlabel/.store       in=\boxlabels,
    ylabel/.store       in=\ylabel,
    zlabel/.store       in=\zlabel,
    caption/.store      in=\caption,
    name/.store         in=\name,
    fill/.store         in=\fill,
    opacity/.store      in=\opacity,
    fill={rgb:red,5;green,5;blue,5;white,15},
    opacity=0.4,
    width=2,
    height=13,
    depth=15,
    scale=.2,
    xlabel={{"","","","","","","","","",""}},
    ylabel=,
    zlabel=,
    caption=,
    name=,
}

可以看出,该文件定义了一个宏,其中存储了可以传递给pic\caption的选项的值。但是,该命令已被包使用。因此,您将在此处收到错误。captionBoxcaption

\mycaption一种解决方案是使用文件中相关宏的另一个名称(例如) sty。因此,您需要更改文件中的两行(第 53 行和第 95 行)Box.sty,使其变为以下内容(请注意,不鼓励使用\bf,因此我将其替换为\textbf):

\ProvidesPackage{Box}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This Block can draw simple block of boxes with custom colors. 
% Can be used for conv, deconv etc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\tikzset{Box/.pic={\tikzset{/boxblock/.cd,#1}
        \tikzstyle{box}=[every edge/.append style={pic actions, densely dashed, opacity=.7},fill opacity=\opacity, pic actions,fill=\fill]
        
        \pgfmathsetmacro{\y}{\cubey*\scale}
        \pgfmathsetmacro{\z}{\cubez*\scale}
   
        %Multiple concatenated boxes
        \foreach[count=\i,%
                 evaluate=\i as \xlabel using {array({\boxlabels},\i-1)},% 
                 evaluate=\unscaledx as \k using {\unscaledx*\scale+\prev}, remember=\k as \prev (initially 0)] 
                 \unscaledx in \cubex
        {
            \pgfmathsetmacro{\x}{\unscaledx*\scale}
            \coordinate (a) at (\k-\x , \y/2 , \z/2); 
            \coordinate (b) at (\k-\x ,-\y/2 , \z/2); 
            \coordinate (c) at (\k    ,-\y/2 , \z/2); 
            \coordinate (d) at (\k    , \y/2 , \z/2); 
            \coordinate (e) at (\k    , \y/2 ,-\z/2); 
            \coordinate (f) at (\k    ,-\y/2 ,-\z/2); 
            \coordinate (g) at (\k-\x ,-\y/2 ,-\z/2); 
            \coordinate (h) at (\k-\x , \y/2 ,-\z/2); 
        
            \draw [box] 
                (d) -- (a) -- (b) -- (c) -- cycle     
                (d) -- (a) -- (h) -- (e) -- cycle
                %dotted edges
                (f) edge (g)
                (b) edge (g)
                (h) edge (g)    
            ;
            \path (b) edge ["\xlabel"',midway] (c);
            
            \xdef\LastEastx{\k} %\k persists as \LastEastx after loop 
        }%Loop ends
        \draw [box] (d) -- (e) -- (f) -- (c) -- cycle; %East face of last box     
        
        \coordinate (a1) at (0 , \y/2 , \z/2);
        \coordinate (b1) at (0 ,-\y/2 , \z/2);
        \tikzstyle{depthlabel}=[pos=0,text width=14*\z,text centered,sloped]       
        
        \path (c) edge ["\small\zlabel"',depthlabel](f); %depth label
        \path (b1) edge ["\ylabel",midway] (a1);  %height label
        
        
        \tikzstyle{captionlabel}=[text width=15*\LastEastx/\scale,text centered]       
        \path (\LastEastx/2,-\y/2,+\z/2) + (0,-25pt) coordinate (cap) 
        edge ["\textcolor{black}{\textbf{\mycaption}}"',captionlabel](cap) ; %Block caption/pic object label
         
        %Define nodes to be used outside on the pic object
        \coordinate (\name-west)   at (0,0,0) ;
        \coordinate (\name-east)   at (\LastEastx, 0,0) ;
        \coordinate (\name-north)  at (\LastEastx/2,\y/2,0);
        \coordinate (\name-south)  at (\LastEastx/2,-\y/2,0);       
        \coordinate (\name-anchor) at (\LastEastx/2, 0,0) ;
        
        \coordinate (\name-near) at (\LastEastx/2,0,\z/2);
        \coordinate (\name-far)  at (\LastEastx/2,0,-\z/2);       
        
        \coordinate (\name-nearwest) at (0,0,\z/2);
        \coordinate (\name-neareast) at (\LastEastx,0,\z/2);
        \coordinate (\name-farwest)  at (0,0,-\z/2);
        \coordinate (\name-fareast)  at (\LastEastx,0,-\z/2);
        
        \coordinate (\name-northeast) at (\name-north-|\name-east);
        \coordinate (\name-northwest) at (\name-north-|\name-west);
        \coordinate (\name-southeast) at (\name-south-|\name-east);
        \coordinate (\name-southwest) at (\name-south-|\name-west);
        
        \coordinate (\name-nearnortheast)  at (\LastEastx, \y/2, \z/2);
        \coordinate (\name-farnortheast)   at (\LastEastx, \y/2,-\z/2);
        \coordinate (\name-nearsoutheast)  at (\LastEastx,-\y/2, \z/2);
        \coordinate (\name-farsoutheast)   at (\LastEastx,-\y/2,-\z/2);
        
        \coordinate (\name-nearnorthwest)  at (0, \y/2, \z/2);
        \coordinate (\name-farnorthwest)   at (0, \y/2,-\z/2);
        \coordinate (\name-nearsouthwest)  at (0,-\y/2, \z/2);
        \coordinate (\name-farsouthwest)   at (0,-\y/2,-\z/2);
        
    },
    /boxblock/.search also={/tikz},
    /boxblock/.cd,
    width/.store        in=\cubex,
    height/.store       in=\cubey,
    depth/.store        in=\cubez,
    scale/.store        in=\scale,
    xlabel/.store       in=\boxlabels,
    ylabel/.store       in=\ylabel,
    zlabel/.store       in=\zlabel,
    caption/.store      in=\mycaption,
    name/.store         in=\name,
    fill/.store         in=\fill,
    opacity/.store      in=\opacity,
    fill={rgb:red,5;green,5;blue,5;white,15},
    opacity=0.4,
    width=2,
    height=13,
    depth=15,
    scale=.2,
    xlabel={{"","","","","","","","","",""}},
    ylabel=,
    zlabel=,
    caption=,
    name=,
}

您需要对sty加载的另外两个文件执行相同的操作init.tex,即Ball.sty(第 21 行和第 28 行需要进行的更改)和RightBandedBox.sty(第 70 行和第 111 行需要进行的更改)。

输出(无错误):

在此处输入图片描述

相关内容