foreach报错如何解决

发布于 2025-07-14 17:07:26
\documentclass[tikz,border=5pt]{standalone}
\usepackage[UTF8]{ctex} % 加载中文支持
\usepackage{pgfplots} % 核心绘图库
\usepackage{bm} % 或 
\usepackage{amsmath}
\usepackage{silence}

% 定义 Times New Roman 字体命令


\WarningFilter{latexfont}{} % 过滤所有字体警告
\pgfplotsset{compat=1.18} % 版本兼容性设置
\usetikzlibrary{positioning,arrows.meta,angles,quotes,matrix,calc,fillbetween,patterns,patterns.meta,math}
\usepgfplotslibrary{fillbetween}

\usepackage{newtxtext, newtxmath}       % Times 风格字体
\usepackage{bm}                         % 加粗数学符号
\newcommand{\bt}[1]{\bm{\mathit{#1}}}   % 自定义加粗倾斜命令


\newcommand{\axisarrow}
{axis line style = {-{Triangle[length=8pt, width=3pt]}},}   % 修正箭头语法


\pgfplotsset{
    tick_empty/.style={
        xtick=\empty,
        ytick=\empty,
        tick style={draw=none},
        major tick length=0pt
    }
}

\begin{document}
\begin{tikzpicture}[scale=1.2, every node/.style={font=\large}, foo /.tip = {Straight Barb[]Bar[width=10pt,sep=+0pt -0.5pt]}]

    % 定义参数(a和b的长度)
    \def\a{5}
    \def\b{1}
    
    % 绘制大正方形边框 (a+b) x (a+b)
    \draw[thick] (0,0) rectangle (\a+\b,\a+\b);
    
    % 绘制内部辅助线
    \draw[thick] (0,\a) -- (\a+\b,\a); % 水平分割线 (y=a)
    \draw[thick] (\a,0) -- (\a,\a+\b); % 垂直分割线 (x=a)
    
    % ===== 标注各部分 =====
    % 左下角正方形 (a x a)
    \draw (0,0) rectangle (\a,\a);
    \node[fill=white, inner sep=1pt] at (0.5*\a, 0.5*\a) {$ A=x_0^2 $};
    
    % 左上角矩形 (a x b)
    \draw[pattern = north east lines, pattern color=gray] (0,\a) rectangle (\a,\a+\b);
    \node[fill=white, inner sep=1pt] at (0.5*\a, \a+0.5*\b) {$x_0 \Delta x$};
    
    % 右下角矩形 (b x a)
    \draw[pattern = north west lines, pattern color=gray] (\a,0) rectangle (\a+\b,\a);
    \node[fill=white, inner sep=1pt] at (\a+0.5*\b, 0.5*\a) {$ x_0\Delta x $};
    
    % 右上角小正方形 (b x b)
    \draw[pattern = crosshatch, pattern color=gray] (\a,\a) rectangle (\a+\b,\a+\b);
    \node[fill=white, inner sep=1pt] at (\a+0.5*\b, \a+0.5*\b) {$ (\Delta x)^2 $};
    
    % ===== 标注边长 =====


    % % 总边长标注(可选)
    \draw[foo-foo] (\a+\b+0.3,0) -- (\a+\b+0.3,\a) node[fill=white, inner sep=1pt,midway] {$ x_0 $};
    \draw[foo-foo] (0,\a+\b+0.3) -- (\a,\a+\b+0.3) node[fill=white, inner sep=1pt,midway] {$ x_0 $};

    % 右上角正方形边长标注
    \draw[foo-foo] (\a+\b+0.3,\a) -- (\a+\b+0.3,\a+\b) node[fill=white, inner sep=1pt,midway] {$ \Delta x $};
    \draw[foo-foo] (\a,\a+\b+0.3) -- (\a+\b,\a+\b+0.3) node[fill=white, inner sep=1pt,midway] {$ \Delta x $};
\end{tikzpicture}

\begin{tikzpicture}%3.1
\begin{axis}[
    axis lines = middle,
    %——xOy
    xlabel = $x$,  
    ylabel = $y$,
    width=8cm, % 根据x轴范围计算宽度
    height=6cm, % 根据y轴范围计算高度
    %将标签定位在x轴的​​最末端​​(刻度坐标系中,1表示100%位置)
    xlabel style={at={(ticklabel* cs:1)}, anchor=north, below=1pt},
    ylabel style={at={(ticklabel* cs:1)}, anchor=east, left=1pt},
    %
    xmin = 0, xmax = 8,
    ymin = 0, ymax = 6,
    \axisarrow                   % 已经有逗号
    xtick={1, 2.5708, 7.2831},
    xticklabels={$a$, $\xi$, $b$},
    tick style={draw=none},       % 隐藏所有刻度线
    ytick=\empty
    ]%不能空行

    \addplot[                
        domain = 1:7.2831,      
        samples = 300,       
        smooth,              
        line width = 0.5pt,  
        black                 
    ] {sin(deg(x-1))+2.5};    
    
    \coordinate (A) at (axis cs:1,2.5);
    \coordinate (B) at (axis cs:7.2831,2.5);
    \coordinate (C) at (axis cs:2.5708,3.5);
    \coordinate (D) at (axis cs:5.7123,1.5);
    
    \foreach \x in {A,B,C}{
        \node[above] at (\i) {$\i$};
    }

    \node[below] at (D) {$D$};


    \draw[lightgray] (A) -- (B);
    \draw[lightgray] (C) ++ (-0.8,0) --++ (1.6,0);
    \draw[lightgray] (D) ++ (-0.8,0) --++ (1.6,0);
    \draw[dash pattern=on 5pt off 2pt] (A) --++ (0,-5);
    \draw[dash pattern=on 5pt off 2pt] (C) --++ (0,-5);
    \draw[dash pattern=on 5pt off 2pt] (B) --++ (0,-5);


    \node[below left] at (axis cs:0,0) {$\bt{O}$};%原点 

\end{axis}

    \node[below=10pt of current bounding box.south, anchor=north]{图\,3.1};
\end{tikzpicture}
\end{document}

查看更多

关注者
0
被浏览
68
Sagittarius Rover
Sagittarius Rover 14小时前
我要成为Typst糕手/(ㄒoㄒ)/~~

提问时尽量给出最小工作示例,摒弃所有与问题无关的内容

这140行代码中90%与你问题无关

好好看看这两天我给你简化了多少代码,这些简化根本不用动脑,只是自己不愿动手。

\documentclass[tikz,border=5pt]{standalone}
\usepackage{pgfplots} % 核心绘图库
\pgfplotsset{compat=1.18} % 版本兼容性设置
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    axis lines = middle,
    xmin = 0, xmax = 8,
    ymin = 0, ymax = 6,
    ]
    \addplot[                
        domain = 1:7.2831,      
        samples = 300,       
        smooth,              
        line width = 0.5pt,  
        black                 
    ] {sin(deg(x-1))+2.5};    
    \coordinate (A) at (axis cs:1,2.5);
    \coordinate (B) at (axis cs:7.2831,2.5);
    \coordinate (C) at (axis cs:2.5708,3.5);
    \coordinate (D) at (axis cs:5.7123,1.5);
    \pgfplotsinvokeforeach {A,B,C}{%
        \node[above] at (#1) {$#1$};
    }
    \node[below] at (D) {$D$};
\end{axis}

\end{tikzpicture}
\end{document}

image.png

2 个回答
123
123 14小时前
这家伙很懒,什么也没写!

image.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览