在 Tikz 中打破循环

在 Tikz 中打破循环
\documentclass{article}
\usepackage{tikz}

\begin{document}


 \foreach \N in {8}
 {\begin{tikzpicture}[scale=1.2,declare function={f(\x)=((1/3)*(\x)^(3)-3* 
            (\x)^(2)+8*\x-3;},
       lnode/.style={fill=white,font=\normalsize,inner sep=0pt,text height=1.5em}]
   \pgfmathtruncatemacro{\M}{1}
    \coordinate (start) at (.8,{f(.8)});
    \foreach \X [remember=\X as \LastX (initially 0)] in {1,2,3,4,...,8}
     \ifnum\X<5
     {\draw[fill=orange!40!white] (1+\LastX*4/\N,0) rectangle (1+\X*4/\N,{f(1+\LastX*4/\N)});
    \draw[red,fill=red] (1+\LastX*4/\N,{f(1+\LastX*4/\N)}) circle (1.4pt) ;
   \path  (1+\LastX*4/\N,0pt) coordinate (x\X);
    \ifnum\X=1
   \draw (1+\LastX*4/\N,3pt) -- (1+\LastX*4/\N,0pt) coordinate (x\X)
   node[anchor=north east,xshift=2pt,lnode]  {$a=x_{\X}$};
  \else
   \pgfmathtruncatemacro{\itest}{mod(\X,\M)}
   \ifnum\itest=0
      \pgfmathsetmacro{\dist}{4-\LastX*4/\N}
      \ifdim\dist cm>5pt
        \draw (1+\LastX*4/\N,3pt) -- (1+\LastX*4/\N,0pt)
         node[anchor=north,lnode] {$x_{\X}$}; 
                \else
        \ifnum\X=8
            \fi
          \fi  
        \fi
      \fi
    \fi
   }
 \draw (5,3pt) -- (5,0pt)
 node[anchor=north west,xshift=-2pt,lnode]{$b$};
 %\draw (1,3pt) -- (1,0pt)
 % node[anchor=north west,xshift=-2pt,lnode]{$a$};
 \node at (5.3,5) [text=red] {$f(x)$};
 \coordinate (end) at (5.05,{f(5.05)});
 \draw (5,3pt) -- (5,0pt) node[anchor=north west,xshift=-2pt,lnode]{$b$};
 \draw (5,0)--(5,{f(5)});
 \draw [-latex] (-0.5,0) -- (6,0) node (xaxis) [below] {$x$};
 \draw [-latex] (0,-0.5) -- (0,5) node [left] {$y$};            
 \draw[domain=.5:5.3,samples=200,variable=\x,red,<->,thick] plot ({\x},{f(\x)});
\node at (5.3,5) [text=red] {$f(x)$};             
\end{tikzpicture}}

\end{document}

这会产生错误但输出:

在此处输入图片描述

我正在尝试使用 Marmot 编写的现有程序来创建

在此处输入图片描述

答案1

你想要这样的东西吗?

\documentclass{article}
\usepackage{tikz}

\begin{document}
 \foreach \N in {8}
 {\begin{tikzpicture}[scale=1.2,declare function={f(\x)=((1/3)*(\x)^(3)-3* 
            (\x)^(2)+8*\x-3;},
       lnode/.style={fill=white,font=\normalsize,inner sep=0pt,text height=1.5em}]
   \pgfmathtruncatemacro{\M}{1}
    \coordinate (start) at (.8,{f(.8)});
    \foreach \X [remember=\X as \LastX (initially 0)] in {1,2,3,4,...,\N}     
     {\ifnum\X<5 %<-level 1
       \draw[fill=orange!40!white] (1+\LastX*4/\N,0) rectangle (1+\X*4/\N,{f(1+\LastX*4/\N)});
       \draw[red,fill=red] (1+\LastX*4/\N,{f(1+\LastX*4/\N)}) circle (1.4pt) ;
       \path  (1+\LastX*4/\N,0pt) coordinate (x\X);
       \ifnum\X=1 %<-level 2
        \draw (1+\LastX*4/\N,3pt) -- (1+\LastX*4/\N,0pt) coordinate (x\X)
         node[anchor=north east,xshift=2pt,lnode]  {$a=x_{\X}$};
       \else
        \pgfmathtruncatemacro{\itest}{mod(\X,\M)}
        \ifnum\itest=0 %<-level 3
         \pgfmathsetmacro{\dist}{4-\LastX*4/\N}
         \ifdim\dist cm>5pt %<-level 4
          \draw (1+\LastX*4/\N,3pt) -- (1+\LastX*4/\N,0pt)
          node[anchor=north,lnode] {$x_{\X}$}; 
         \fi %<-level 4 
        \fi %<-level 3
       \fi %<-level 2
      \else
       \ifnum\X=\N %<-level 2
        \draw[fill=orange!40!white] (1+\LastX*4/\N,0) rectangle (1+\X*4/\N,{f(1+\LastX*4/\N)});
        \draw[red,fill=red] (1+\LastX*4/\N,{f(1+\LastX*4/\N)}) circle (1.4pt) ;
        \path (1+4*4/\N,{f(1+\LastX*4/\N)/2}) -- (1+\LastX*4/\N,{f(1+\LastX*4/\N)/2}) 
            node[midway,font=\huge\bfseries]{\dots};
       \fi %<-level 2
      \fi %<-level 1
   }
 \draw (5,3pt) -- (5,0pt)
 node[anchor=north west,xshift=-2pt,lnode]{$b$};
 %\draw (1,3pt) -- (1,0pt)
 % node[anchor=north west,xshift=-2pt,lnode]{$a$};
 \node at (5.3,5) [text=red] {$f(x)$};
 \coordinate (end) at (5.05,{f(5.05)});
 \draw (5,3pt) -- (5,0pt) node[anchor=north west,xshift=-2pt,lnode]{$b$};
 \draw (5,0)--(5,{f(5)});
 \draw [-latex] (-0.5,0) -- (6,0) node (xaxis) [below] {$x$};
 \draw [-latex] (0,-0.5) -- (0,5) node [left] {$y$};            
 \draw[domain=.5:5.3,samples=200,variable=\x,red,<->,thick] plot ({\x},{f(\x)});
\node at (5.3,5) [text=red] {$f(x)$};             
\end{tikzpicture}}
\end{document}

在此处输入图片描述

相关内容