与副标题和子列表冲突吗?

与副标题和子列表冲突吗?

下面的代码部分来自从 pgfplots groupstyle 中拆分子图,当我包含子列表环境时,我无法编译我的代码。我需要此列表来进行 Python 代码子列表。

你能帮助我解决我收到的错误吗:

! LaTeX Error: Command \c@sublisting already defined.
               Or name \end... illegal, see p.192 of the manual.

以下是代码:

\documentclass[11pt]{book}
\usepackage[%
    ,top=3cm
    ,bottom=3cm
    ,left=3.2cm
    ,right=3.2cm
    ,headsep=10pt
    ,a4paper
    ]{geometry}
\usepackage{lipsum}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{%
    ,compat=1.12
    ,minor grid style={dashed,red}
    ,major grid style={dotted,green!50!black}
    }
\usepackage{caption,subcaption}
\captionsetup[subfigure]{labelfont=it,textfont=it,labelformat=parens,labelsep=space}
\usepackage{siunitx}
\usepackage{listings}

%========================================================================================
%   Python and C++ Code Environment
%========================================================================================

\usepackage[pygopt={texcomments=true,style=emacs}]{pythontex}
\setpythontexlistingenv{listing}

\newcounter{sublisting}[listing]
\newcommand{\codeline}[1]{%
  \addcontentsline{lopytx}{listing}%
    {\protect\numberline{\hspace{0.5in}\thelisting.\arabic{FancyVerbLine}}\hspace{0.5in}#1}%
}

\begin{document}
\listoffigures
\lipsum[1]
\begin{figure}[h]
    \centering
    \begin{tikzpicture}
        \setcaptionsubtype
        \begin{groupplot}[%
            ,group style={%
                ,group name=my plots
                ,group size=2 by 3
                ,vertical sep=2cm,
                ,horizontal sep = 2cm,
                ,ylabels at=edge left
                }
            ,width=7cm
            ,height=6cm
            ,try min ticks=5
            ,xlabel={Frequency in \si{\hertz}}
            ,grid=both
            ,every major grid/.style={gray, opacity=0.5}
            ]
            \nextgroupplot%
                \addplot[smooth,blue]{rnd};
            \nextgroupplot%
                \addplot[smooth,blue] {rnd};%
                \addplot[mark=*,red,mark options={scale=.65}] {rnd};
        \end{groupplot}
        \node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r1.north) {\caption{Plot \arabic{subfigure}\label{subplot:one}}};
        \node[text width=.5\linewidth,align=center,anchor=south] at (my plots c2r1.north) {\caption{Plot \arabic{subfigure}\label{subplot:two}}};
    \end{tikzpicture}
    \caption{Plot showing Absolute Errors.}
\end{figure}
\begin{figure}
    \ContinuedFloat
    \centering
    \begin{tikzpicture}
        \setcaptionsubtype
        \begin{groupplot}[%
            ,group style={%
                ,group name=my plots
                ,group size=2 by 3
                ,vertical sep=2cm,
                ,horizontal sep = 2cm,
                ,ylabels at=edge left
            }
            ,width=7cm
            ,height=6cm
            ,try min ticks=5
            ,xlabel={Frequency in \si{\hertz}}
            ,grid=both
            ,every major grid/.style={gray, opacity=0.5}
            ]
            \nextgroupplot%
                \addplot[smooth,blue] {rnd};
            \nextgroupplot%
                \addplot[smooth,blue] {rnd};%
                \addplot[mark=*,red,mark options={scale=.65}] {rnd};
            \nextgroupplot%
                \addplot[smooth,blue] {rnd};
            \nextgroupplot%
                \addplot[smooth,blue] {rnd};%
                \addplot[mark=*,red,mark options={scale=.65}] {rnd};
        \end{groupplot}
        \node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r1.north) {\caption[]{Plot \arabic{subfigure}\label{subplot:three}}};
        \node[text width=.5\linewidth,align=center,anchor=south] at (my plots c2r1.north) {\caption[]{Plot \arabic{subfigure}\label{subplot:four}}};
        \node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r2.north) {\caption[]{Plot \arabic{subfigure}\label{subplot:five}}};
        \node[text width=.5\linewidth,align=center,anchor=south] at (my plots c2r2.north) {\caption[]{Plot \arabic{subfigure}\label{subplot:six}}};
    \end{tikzpicture}
    \caption[]{Plot showing Absolute Errors. (continued)}\label{abserror}
\end{figure}
\end{document} 

答案1

解决方案:只需放下电话线

\newcounter{sublisting}[listing]

一切都应该没问题。这不是subcaptionpythontex包之间的冲突,恰恰相反,这是一个你实际上不需要此行的功能。

实际上这一行有两次是错误的:

  1. \DeclareCaptionSubType{listing}

才是正确的行,以使subcaption包能够感知listing环境。否则,诸如此类的功能\ContinuedFloat将无法正常工作。并且

  1. 软件包subcaption会自动\DeclareCaptionSubType为软件包束已知的每个浮动环境执行此操作(即定义子计数器)caption。因此,尝试重新定义已经存在的计数器sublisting会导致此处出错。

但是软件包bundle如何caption知道浮动环境呢listing?这是因为pythontex软件包使用软件包\DeclareFloatingEnvironment提供的newfloat来定义其浮动环境listing,而newfloat软件包是软件包bundle的一部分caption。所以事实上

\setpythontexlistingenv{listing}

sublisting如果使用该包,则为您另外定义计数器subcaption,无需自己执行此操作。

在包装手册中可以找到关于此问题的提示subcaption,解释如下\DeclareCaptionSubType

“对于环境图形和表格,以及使用 newfloat 包提供的 \DeclareFloatingEnvironment 定义的所有环境,这将自动完成,但对于其他环境(例如,使用 float 包提供的 \newfloat 或 floatrow 包提供的 \DeclareNewFloatType 定义的环境)这必须手动完成。”

相关内容