将 geogebra 的 pgf/tikz 图形导出到 latex 时出现问题

将 geogebra 的 pgf/tikz 图形导出到 latex 时出现问题

当我尝试导出该图形时,我得到了仅带有坐标轴的图形。

\documentclass[10pt]{article}
\usepackage{pgf,tikz,pgfplots}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\begin{axis}[
x=1.0cm,y=1.0cm,
axis lines=middle,
ymajorgrids=true,
xmajorgrids=true,
xmin=-3.9999999999999982,
xmax=3.979999999999998,
ymin=-2.9800000000000013,
ymax=2.9800000000000013,
xtick={-3.0,-2.0,...,3.0},
ytick={-2.0,-1.0,...,2.0},]
\clip(-4.,-2.98) rectangle (3.98,2.98);
\draw[line width=0.8pt,smooth,samples=100,domain=6.920000002293365E-6:0.9999961013223213] plot(\x,{sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\draw[line width=0.8pt,smooth,samples=100,domain=2.000002653901591:3.979999999999998] plot(\x,{sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\draw[line width=0.8pt,smooth,samples=100,domain=6.920000002293365E-6:0.9999961013223213] plot(\x,{0-sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\draw[line width=0.8pt,smooth,samples=100,domain=2.000002653901591:3.979999999999998] plot(\x,{0-sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\end{axis}
\end{tikzpicture}
\end{document}

答案1

最快的解决方法是添加\pgfplotsset{compat=1.11}到序言中。

默认情况下,正常 TikZ 绘图宏的坐标\draw如下不是在环境中使用时,对应于轴单位。但是,axis从的兼容性设置开始,默认设置会发生变化,因此坐标确实对应于轴坐标,而不是底层的坐标。(我认为这至少是原因,尽管我并不完全确定。)1.11pgfplotstikzpicture


话虽如此,我个人宁愿改变代码以使用\addplotpgfplots而不是\draw plot,即

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45]
\begin{axis}[
axis lines=middle,
ymajorgrids=true,
xmajorgrids=true,
xmin=-3.9999999999999982,
xmax=3.979999999999998,
ymin=-2.9800000000000013,
ymax=2.9800000000000013,
xtick={-3.0,-2.0,...,3.0},
ytick={-2.0,-1.0,...,2.0}
]

\addplot [line width=0.8pt,smooth,samples=100,domain=6.920000002293365E-6:0.9999961013223213] (\x,{sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\addplot [line width=0.8pt,smooth,samples=100,domain=2.000002653901591:3.979999999999998] (\x,{sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\addplot [line width=0.8pt,smooth,samples=100,domain=6.920000002293365E-6:0.9999961013223213] (\x,{0-sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\addplot[line width=0.8pt,smooth,samples=100,domain=2.000002653901591:3.979999999999998] (\x,{0-sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\end{axis}
\end{tikzpicture}

顺便说一句,\usepackage{pgf,tikz,pgfplots}有点冗余,pgfplots加载tikz,加载pgf,所以你真正需要的只需要\usepackage{pgfplots}

完整示例和输出:

在此处输入图片描述

\documentclass[10pt]{article}
\usepackage{pgfplots}

\usetikzlibrary{arrows}
\pagestyle{empty}
\begin{document}
Modified code:

\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45]
\begin{axis}[
axis lines=middle,
ymajorgrids=true,
xmajorgrids=true,
xmin=-3.9999999999999982,
xmax=3.979999999999998,
ymin=-2.9800000000000013,
ymax=2.9800000000000013,
xtick={-3.0,-2.0,...,3.0},
ytick={-2.0,-1.0,...,2.0}
]

\addplot [line width=0.8pt,smooth,samples=100,domain=6.920000002293365E-6:0.9999961013223213] (\x,{sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\addplot [line width=0.8pt,smooth,samples=100,domain=2.000002653901591:3.979999999999998] (\x,{sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\addplot [line width=0.8pt,smooth,samples=100,domain=6.920000002293365E-6:0.9999961013223213] (\x,{0-sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\addplot[line width=0.8pt,smooth,samples=100,domain=2.000002653901591:3.979999999999998] (\x,{0-sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\end{axis}
\end{tikzpicture}

With \texttt{compat=1.11}:

% in general you want this line in the preamble, I added it here just for the sake
% of the example, to show that the above code works without it
\pgfplotsset{compat=1.11}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\begin{axis}[
x=1.0cm,y=1.0cm,
axis lines=middle,
ymajorgrids=true,
xmajorgrids=true,
xmin=-3.9999999999999982,
xmax=3.979999999999998,
ymin=-2.9800000000000013,
ymax=2.9800000000000013,
xtick={-3.0,-2.0,...,3.0},
ytick={-2.0,-1.0,...,2.0},]
\clip(-4.,-2.98) rectangle (3.98,2.98);
\draw[line width=0.8pt,smooth,samples=100,domain=6.920000002293365E-6:0.9999961013223213] plot(\x,{sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\draw[line width=0.8pt,smooth,samples=100,domain=2.000002653901591:3.979999999999998] plot(\x,{sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\draw[line width=0.8pt,smooth,samples=100,domain=6.920000002293365E-6:0.9999961013223213] plot(\x,{0-sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\draw[line width=0.8pt,smooth,samples=100,domain=2.000002653901591:3.979999999999998] plot(\x,{0-sqrt(((\x)*((\x)-1.0)*((\x)-2.0))/6.0)});
\end{axis}
\end{tikzpicture}

\end{document}

相关内容