答案1
使用纯 TikZ:
\documentclass[tikz, margin=3.14159 mm]{standalone}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
% axis
\draw[-Straight Barb] (-1,0) -- (9,0) node[right] {$n$};
\draw[-Straight Barb] (0,-3) -- (0,3) node[above] {$v_2(a_n)$};
% ticks and dashed lines, drawn in loop
\foreach \i [count=\j from 0] in {1,2,4,8}
\draw[densely dashed] (\i,0) node [above] {\i} -- (\i,-\j) coordinate (x\j);
\foreach \i/\j/\k in {-1/-1/3, 0/0/2, 1/1/1, 2/$v_p(\alpha)$/0}
\path[draw=red, dashed] (0,\i) node[left,fill=white, inner sep=2pt] {\j} -- (x\k);
% function
\draw[thick, green] plot coordinates {(0,2) (1,0) (2,-1)};
\draw[thick, red] plot coordinates {(2,-1) (4,-2) (8,-3)};
\end{tikzpicture}
\end{document}
您可以在本地安装的 LaTeX 中编译代码,也可以使用背页服务。
编辑:
上面的 MWE 工作独立于所使用的\documentclass
。例如,如果你使用amsart
包,那么第一行代码
\documentclass[tikz, margin=3.14159 mm]{standalone}
用。。。来代替
\documentclass[12pt,leqno]{amsart}
\usepackage{tikz}
结果与之前完全相同,没有错误,没有警告,也没有坏框消息。
如果您希望将上图作为带有标题的图形包含在文档中,则可以将其插入figure
浮动环境中并添加标题,如下所示:
\documentclass[12pt,leqno]{amsart}
\usepackage{pgfplots} % it also load tikz package
\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows.meta}
\usepackage{caption}
\usepackage[colorlinks=true,
citecolor={black}]{hyperref}
\usepackage{lipsum} % added for generating a dummy text,
% not needed in real document
\begin{document}
\lipsum[1] % dummy text
\begin{figure}[ht] % placed here or on the top of page
\begin{tikzpicture}
% axis
\draw[-Straight Barb] (-1,0) -- (9,0) node[right] {$n$};
\draw[-Straight Barb] (0,-3) -- (0,3) node[above] {$v_2(a_n)$};
\foreach \i [count=\j from 0] in {1,2,4,8}
\draw[densely dashed] (\i,0) node [above] {\i} -- (\i,-\j) coordinate (x\j);
\foreach \i/\j/\k in {-1/-1/3, 0/0/2, 1/1/1, 2/$v_p(\alpha)$/0}
\path[draw=red, dashed] (0,\i) node[left,fill=white, inner sep=2pt] {\j} -- (x\k);
% function
\draw[thick, green] plot coordinates {(0,2) (1,0) (2,-1)};
\draw[thick, red] plot coordinates {(2,-1) (4,-2) (8,-3)};
\end{tikzpicture}
\caption{Caption of the figure} % caption
\label{fig:diagram} % for referencing of figure, key select as you wish
\end{figure}
\lipsum[2] % dummy text
For further explanation see figure \ref{fig:diagram} \dots % referencing of figure
\end{document}
给出以下结果(显示的是页面的一部分):
正如所料,这个 MWE 的编译也没有错误。
答案2
并且与tikz
和 一起pgfplots
:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[mystyle/.style={semithick},
axis x line=center,
axis y line=center,
xtick={0,1,2,4,8},
ytick={-1,1},
extra y ticks={3},
extra y tick labels={$\nu_p(\alpha)$},
xlabel={$n$},
ylabel={$\nu_2(a_n)$},
xlabel style={right},
ylabel style={above},
x tick label style={anchor=south,above,yshift=1ex},
yticklabel style={red},
extra y tick style={yticklabel style={color={black}}},
xmin=-1,
xmax=9,
ymin=-4,
ymax=4]
% Main lines
\addplot[mystyle,green,thick]coordinates{(0,3)(1,0)(2,-1)};
\addplot[mystyle,orange,thick]coordinates{(2,-1)(4,-2)};
\addplot[mystyle,thick]coordinates{(4,-2)(8,-3)};
% red dashed help lines
\addplot[mystyle,dashed,red]coordinates{(0,1)(1,0)};
\addplot[mystyle,dashed,red]coordinates{(0,0)(2,-1)};
\addplot[mystyle,dashed,red]coordinates{(0,-1)(4,-2)};
% vertical dashed lines
\addplot[mystyle,dashed]coordinates{(2,0)(2,-1)};
\addplot[mystyle,dashed]coordinates{(4,0)(4,-2)};
\addplot[mystyle,dashed]coordinates{(8,0)(8,-3)};
\node at (0,0) [above,anchor=south west,shift={(-0.075cm,0.075cm)}] {0};
\end{axis}
\end{tikzpicture}
\end{document}