\pgfplotsinvokeforeach 向变量添加一个

\pgfplotsinvokeforeach 向变量添加一个

我尝试使用 a 中的变量\pgfplotsinvokeforeach作为路径名。我想在每次迭代中获取和#1#1 + 1我无法让工作#1 + 1;它尝试使用我写的字面意思,而不是计算值。

\documentclass[11pt]{article}                                                    
\usepackage[top=2.54cm,bottom=2.54cm,left=2.75cm,right=2.75cm]{geometry}         
\usepackage{tikz}                                                                
\usetikzlibrary{calc}                                                            
\usepackage{pgfplots}                                                            
\usepgfplotslibrary{fillbetween}                                                 
\pgfplotsset{compat=1.17}                                                        
                                                                                 
\begin{filecontents}{data.txt}                                                   
0 10000                                                                          
1 15000                                                                          
2 10000                                                                          
3 20000                                                                          
4 5000                                                                           
5 0                                                                              
\end{filecontents}                                                               
\begin{document}                                                                 
\begin{figure}                                                                   
    \centering                                                                   
    \begin{tikzpicture}                                                          
        \begin{axis} [                                                           
                height = \axisdefaultheight,                                     
                width = \textwidth,                                              
                xlabel = Channel,                                                
                ylabel = Counts,                                                 
                ymin = 0,                                                        
                xmin = -5,                                                       
                xmax = 132,                                                      
                xtick pos = left,                                                
                axis on top,                                                     
            ]                                                                    
            % Vertical division of each Board [0-7]                              
            \path  (current axis.above origin) coordinate (T);                   
            \pgfplotsinvokeforeach{0,...,8}{                                     
                \draw[draw=black!5, name path=line#1]  (16*#1-0.5,0) -- (16*#1-0.5,0|-T);
            }                                                                    
            \path[name path=line9] (\pgfkeysvalueof{/pgfplots/xmax},0) -- (\pgfkeysvalueof{/pgfplots/xmax}, 0|-T);
            \pgfplotsinvokeforeach{0,2,4,6,8} {                                  
                \addplot [fill=black!5] fill between[of=line#1 and               
                line#1];                                                         
            }                                                                    
                                                                                 
            \addplot[                                                            
                ybar interval,                                                   
                x filter/.code=\pgfmathparse{#1-0.5},                            
                fill = black!20] table {data.txt};                               
        \end{axis}                                                               
    \end{tikzpicture}                                                            
    \caption{Plot.}                                                              
\end{figure}                                                                     
\end{document}  

答案1

您可以使用\the\numexpr#1+1

\documentclass[11pt]{article}                                                    
\usepackage[top=2.54cm,bottom=2.54cm,left=2.75cm,right=2.75cm]{geometry}         
\usepackage{tikz}                                                                
\usetikzlibrary{calc}                                                            
\usepackage{pgfplots}                                                            
\usepgfplotslibrary{fillbetween}                                                 
\pgfplotsset{compat=1.17}                                                        
                                                                                 
\begin{filecontents}{data.txt}                                                   
0 10000                                                                          
1 15000                                                                          
2 10000                                                                          
3 20000                                                                          
4 5000                                                                           
5 0                                                                              
\end{filecontents}                                                               
\begin{document}                                                                 
\begin{figure}                                                                   
    \centering                                                                   
    \begin{tikzpicture}                                                          
        \begin{axis} [                                                           
                height = \axisdefaultheight,                                     
                width = \textwidth,                                              
                xlabel = Channel,                                                
                ylabel = Counts,                                                 
                ymin = 0,                                                        
                xmin = -5,                                                       
                xmax = 132,                                                      
                xtick pos = left,                                                
                axis on top,                                                     
            ]                                                                    
            % Vertical division of each Board [0-7]                              
            \path  (current axis.above origin) coordinate (T);                   
            \pgfplotsinvokeforeach{0,...,8}{                                     
                \draw[draw=black!5, name path=line#1]  (16*#1-0.5,0) -- (16*#1-0.5,0|-T);
            }                                                                    
            \path[name path=line9] (\pgfkeysvalueof{/pgfplots/xmax},0) -- (\pgfkeysvalueof{/pgfplots/xmax}, 0|-T);
            \pgfplotsinvokeforeach{0,2,4,6} {                                  
                \addplot [fill=black!5] fill between[of=line#1 and               
                line\the\numexpr#1+1\relax];                                                         
            }                                                                    
                                                                                 
            \addplot[                                                            
                ybar interval,                                                   
                x filter/.code=\pgfmathparse{#1-0.5},                            
                fill = black!20] table {data.txt};                               
        \end{axis}                                                               
    \end{tikzpicture}                                                            
    \caption{Plot.}                                                              
\end{figure}                                                                     
\end{document}  

在此处输入图片描述

相关内容