在条形图中使用计数器

在条形图中使用计数器

我有一份包含多变量微积分不同主题的文档。我想在文档开头添加一个条形图,根据文档中的问题数量显示每种问题的数量。换句话说,我希望条形图显示在下方

在此处输入图片描述

根据文档的内容和节数自动生成。然而,我在尝试实现自动化时遇到了很多问题。

  • 我无法将条形图放在文档开头,因为尚未定义用于计数子部分的计数器。可以totcount在这里完成这项工作吗?
  • symbolic y coords我无法使用循环创建内容foreach
  • Nameref 内部不起作用symbolic y coords

任何帮助解决这三个问题的帮助都将不胜感激。

\documentclass[a4paper, norsk, 12pt]{article}

\usepackage{xparse}
\usepackage{nameref}
\newcommand{\seksjon}[1]{\section{#1}\label{sec:\arabic{section}}\newcounter{problem\arabic{section}}}
% Defines a simple bold text, \oppgave[text] produces Oppgave 1 (text). 
\NewDocumentCommand{\oppgave}{o}{%
  % <code>%
  \noindent\\
  % \textbf{\large Oppgave~\stepcounter{problemMA1103}\arabic{problemMA1103}}
  \textbf{\large Oppgave~\stepcounter{problem\arabic{section}}\arabic{problem\arabic{section}}}
  \IfNoValueTF{#1}
  {}
  {(#1)}%
  \medskip\\\noindent
  % <code>
}
\usepackage{totcount}
\regtotcounter{section}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\seksjon{Integrals}

\oppgave

\seksjon{Derivatives}

\oppgave
\oppgave
\oppgave
\seksjon{Green's theorem}

\oppgave
\oppgave
\seksjon{Gauss' Theorem}

\oppgave
\oppgave
\seksjon{Stokes Theorem}

\oppgave
\oppgave
\oppgave
\oppgave


The total number of chapters is \total{section}, in \nameref{sec:1} there are
\arabic{problem1} problems and in \nameref{sec:5} there are
\arabic{problem5} problems.

\begin{tikzpicture}
  \foreach \i in {1,...,\arabic{section}}
  {
    \node at (0, -\i) {\nameref{sec:\i}: \arabic{problem\i}};
  }
\end{tikzpicture} 

\begin{figure}[htbp!]
  \centering
\begin{tikzpicture}
    \begin{axis}[
    xbar,
    y=-0.5cm,
    bar width=0.3cm,
    y axis line style = { opacity = 0 },
    axis x line       = none,
    tickwidth         = 0pt,
    enlarge y limits  = 0.2,
    enlarge x limits  = 0.02,
    ytick = data,
    nodes near coords,
    symbolic y coords = {%
      Integrals, 
      Derivatives,
      Green's theorem,
      Gauss' theorem, 
      Stokes theorem}
    ]
    \addplot[fill=black] coordinates {%
      (\arabic{problem1},Integrals)
      (\arabic{problem2},Derivatives)
      (\arabic{problem3},Green's theorem)  
      (\arabic{problem4},Gauss' theorem)  
      (\arabic{problem5},Stokes theorem)};
%    \legend{Topics, Posts}
    \end{axis}
    \end{tikzpicture}
  \end{figure}

\end{document}

答案1

有些解决方法简单,有些则不那么简单。主要方法是使用refcount以提供 的可扩展版本\nameref

另一个技巧是仅在计数器注册后才排版表格和图形,否则第一次运行 LaTeX 时会出现错误。

\documentclass[a4paper,norsk,12pt]{article}

\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{xparse}
\usepackage{nameref}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{refcount}

\newcommand*{\enameref}[1]{%
  \getrefbykeydefault{#1}{name}{}%
}

\newcommand{\seksjon}[1]{%
  \section{#1}\label{sec:\arabic{section}}%
  \newcounter{problem\arabic{section}}%
  \begingroup\edef\x{\endgroup
    \noexpand\regtotcounter{problem\arabic{section}}}%
  \x
}
% Defines a simple bold text, \oppgave[text] produces Oppgave 1 (text). 
\NewDocumentCommand{\oppgave}{o}{%
  \par\addvspace{\medskipamount}%
  \stepcounter{problem\arabic{section}}%
  \noindent\textbf{\large Oppgave~\arabic{problem\arabic{section}}}
  \IfValueTF{#1}{(#1)}%
  \par\addvspace{\medskipamount}%
}
\usepackage{totcount}
\regtotcounter{section}

\begin{document}

\ifcsname c@problem1@totc\endcsname
The total number of chapters is \total{section}, in \nameref{sec:1} there are
\total{problem1} problems and in \nameref{sec:5} there are
\total{problem5} problems.

\begin{tikzpicture}
  \foreach \i in {1,...,\number\totvalue{section}}
  {
    \node at (0, -\i) {\nameref{sec:\i}: \number\totvalue{problem\i}};
  }
\end{tikzpicture}

\begin{tikzpicture}
  \begin{axis}[
    xbar,
    y=-0.5cm,
    bar width=0.3cm,
    y axis line style = { opacity = 0 },
    axis x line       = none,
    tickwidth         = 0pt,
    enlarge y limits  = 0.2,
    enlarge x limits  = 0.02,
    ytick = data,
    nodes near coords,
    symbolic y coords/.expanded = {
      \enameref{sec:1}, 
      \enameref{sec:2},
      \enameref{sec:3},
      \enameref{sec:4},
      \enameref{sec:5},
    }
  ]
  \begingroup\edef\x{\endgroup
    \noexpand\addplot[fill=black] coordinates {%
      (\number\totvalue{problem1},\enameref{sec:1})
      (\number\totvalue{problem2},\enameref{sec:2})
      (\number\totvalue{problem3},\enameref{sec:3})  
      (\number\totvalue{problem4},\enameref{sec:4})  
      (\number\totvalue{problem5},\enameref{sec:5})
    };
  }\x
%   \legend{Topics, Posts}
    \end{axis}
    \end{tikzpicture}
\fi


\seksjon{Integrals}
\oppgave

\seksjon{Derivatives}
\oppgave
\oppgave
\oppgave

\seksjon{Green's theorem}
\oppgave
\oppgave

\seksjon{Gauss' Theorem}
\oppgave
\oppgave

\seksjon{Stokes' Theorem}
\oppgave
\oppgave
\oppgave
\oppgave

\end{document}

在此处输入图片描述

答案2

  • 这是一个未完成尝试解决问题。
  • 我会使用计数器,然后将计数器值存储在 a 中\command,然后在里面使用它pgfplots
  • 但我并没有解决这个问题。文档开头的计数器值始终为零。也许可以像在目录生成中存储的那样存储它 (@experts)。

\documentclass{article}

\newcounter{myCatACount}[section]
\newcounter{myCatBCount}[section]
\newcounter{myCatCCount}[section]

\begin{document}

\section*{pgfplots}

\newcommand{\myValueA}{\themyCatACount} % \the "CounterName"
\newcommand{\myValueB}{\themyCatBCount}
\newcommand{\myValueC}{\themyCatBCount}

\begin{itemize}
\item \texttt{myValueA:} \myValueA
\item \texttt{myValueB:} \myValueB
\item \texttt{myValueC:} \myValueC
\end{itemize}


\section{Cat A}
    \subsection{Probem A.1}
    \stepcounter{myCatACount}
    \texttt{myCatACount:} \arabic{myCatACount}

    \subsection{Probem A.2}
    \stepcounter{myCatACount}
    \texttt{myCatACount:} \arabic{myCatACount}

    \subsection{Probem A.3}
    \stepcounter{myCatACount}
    \texttt{myCatACount:} \arabic{myCatACount}


\section{Cat B}
    \subsection{Probem B.1}
    \stepcounter{myCatBCount}
    \texttt{myCatBCount:} \arabic{myCatBCount}

    \subsection{Probem B.2}
    \stepcounter{myCatBCount}
    \texttt{myCatBCount:} \arabic{myCatBCount}

\section{Cat C}
    \subsection{Probem C.1}
    \stepcounter{myCatCCount}
    \texttt{myCatCCount:} \arabic{myCatCCount}

    \subsection{Probem C.2}
    \stepcounter{myCatACount}
    \texttt{myCatCCount:} \arabic{myCatCCount}

    \subsection{Probem C.3}
    \stepcounter{myCatCCount}
    \texttt{myCatCCount:} \arabic{myCatCCount}

\end{document}

在此处输入图片描述

相关内容