保存 tikz 图像并在 latex 之外使用

保存 tikz 图像并在 latex 之外使用

我需要创建并保存乳胶图纸,以便可以在其他地方使用它。

我正在使用代码

\begin{tikzpicture}[scale=0.8,domain=-2:20]
\begin{axis}[
height = 11.7cm,
width = 15cm,
  axis lines=middle,
  grid=major,
  xmin=-2,
  xmax=10,
  ymin=-2,
  ymax=20,
   y label style={at={(axis description cs:0.5,-0.05)},anchor=north},
    x label style={at={(axis description cs:-0.05,.5)},rotate=0,anchor=south},
  xlabel=$y$,
  ylabel=$x$,
  xtick={-2,-1,0,...,14},
  ytick={-2,0,...,20},
  tick style={very thick},
  legend style={
  at={(rel axis cs:0,1)},
  anchor=north west,draw=none,inner sep=0pt,fill=gray!10}
]
\addplot[black,thick,samples=1] {0};
\draw[color=red, smooth] plot (\x,\x*\x-8*\x+16) node[above left] {$y = 3$};
\draw (4,1.5) node[anchor=north]{(4,0)};
\draw (1.2,16) node[anchor=east]{(0,16)};
\draw (7,8)  node[anchor=west]{$y=x^2-8x+16$};
\end{axis}
\end{tikzpicture}  

我想将它创建的图像保存为 png。我需要创建多个这样的图像,每个图像都有差异。所以我需要以精确的比例和大小创建所有图像,因此对页面进行截图是不够的。

根据评论,我开发了代码,包括我原始文档的序言。

\documentclass[tikz,convert]{standalone} 
\usepackage[colorlinks=true]{hyperref}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{graphicx}
\usepackage[subpreambles=true]{standalone}
\usepackage{import}
\usepackage{mathtools}
\usepackage{tkz-euclide}
\usepackage{bigints}
\usetkzobj{all}
\usepackage{verbatim}
\usepackage{array}
\usepackage{float}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{siunitx}
\usepackage{gensymb}


\usepackage{xlop}

\usepackage{color,soul}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage[a4paper,top=3cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.5cm]{geometry}
\usetikzlibrary{intersections}
\sisetup{output-decimal-marker={,}}
\pgfplotsset{compat=1.14}
\setlength\parindent{0pt}
\setlength{\parskip}{2em}
\renewcommand{\arraystretch}{1.9}

%need for table solutions
\usepackage{xcolor}
\CorrectChoiceEmphasis{\color{red}}
\setlength\fillinlinelength{1cm}
\setlength\answerclearance{0.5ex}

\newcommand{\NN}{\mathbb{N}}
\newcommand{\ZZ}{\mathbb{Z}}
\newcommand{\CC}{\mathbb{C}}
\newcommand{\RR}{\mathbb{R}}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\newcommand{\sinth}{\sin(\theta)}
\newcommand{\costh}{\cos(\theta)}
\newcommand{\tanth}{\tan(\theta)}
\newcommand{\secth}{\sec(\theta)}
\newcommand{\cosecth}{\cosec(\theta)}
\newcommand{\coshth}{\cosh(\theta)}
\newcommand{\sinhth}{\sinh(\theta)}
\newcommand{\tanhth}{\tanh(\theta)}

\DeclareMathOperator{\sininv}{sin^{-1}}
\DeclareMathOperator{\cosinv}{cos^{-1}}
\DeclareMathOperator{\taninv}{tan^{-1}}

\theoremstyle{definition}
\newtheorem{exercise}{Exercise}[section]
\theoremstyle{definition}
\newtheorem{example}{Example}[section]





\begin{document}
\begin{tikzpicture}[domain=-2:20, range=-2:10]
\begin{axis}[
  axis lines=middle,
  grid=major,
  xmin=-2,
  xmax=10,
  ymin=-2,
  ymax=20,
   y label style={at={(axis description cs:0.5,-0.05)},anchor=north},
    x label style={at={(axis description cs:-0.05,.5)},rotate=0,anchor=south},
  xlabel=$y$,
  ylabel=$x$,
  xtick={-2,-1,0,...,14},
  ytick={-2,0,...,20},
  tick style={very thick},
  legend style={
  at={(rel axis cs:0,1)},
  anchor=north west,draw=none,inner sep=0pt,fill=gray!10}
]
\addplot[black,thick,samples=1] {0};
\draw[color=red, smooth] plot (\x,\x*\x-8*\x+16) node[above left] {$y = 3$};
\draw (4,1.5) node[anchor=north]{(4,0)};
\draw (1.2,16) node[anchor=east]{(0,16)};
\draw (7,8)  node[anchor=west]{$y=x^2-8x+16$};
\end{axis}
\end{tikzpicture}  
\end{document}

这样可以创建我的图表,但不在页面的中心,并且图表超出页面范围。

在此处输入图片描述

使用下面给出的代码来计算方程时,它运行完美。但是,如果我尝试绘制另一个图表,它会超出页面。 在此处输入图片描述

答案1

我很乐意删除它,但如果我编译

\documentclass[tikz,convert]{standalone} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.16} 
\begin{document}
\begin{tikzpicture}[scale=0.8,domain=-2:20]
\begin{axis}[
height = 11.7cm,
width = 15cm,
  axis lines=middle,
  grid=major,
  xmin=-2,
  xmax=10,
  ymin=-2,
  ymax=20,
   y label style={at={(axis description cs:0.5,-0.05)},anchor=north},
    x label style={at={(axis description cs:-0.05,.5)},rotate=0,anchor=south},
  xlabel=$y$,
  ylabel=$x$,
  xtick={-2,-1,0,...,14},
  ytick={-2,0,...,20},
  tick style={very thick},
  legend style={
  at={(rel axis cs:0,1)},
  anchor=north west,draw=none,inner sep=0pt,fill=gray!10}
]
\addplot[black,thick,samples=1] {0};
\draw[color=red, smooth] plot (\x,\x*\x-8*\x+16) node[above left] {$y = 3$};
\draw (4,1.5) node[anchor=north]{(4,0)};
\draw (1.2,16) node[anchor=east]{(0,16)};
\draw (7,8)  node[anchor=west]{$y=x^2-8x+16$};
\end{axis}
\end{tikzpicture}  
\end{document}

pdflatex -shell-escape得到了正确大小的 png 文件。

感谢您添加序言。不幸的是,您的代码会抛出错误,并且与此相关的是,序言远远不够简洁。在剥离与以下内容无关的所有内容后,结果tikzpicture

\documentclass[tikz,convert]{standalone} 
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}[domain=-2:20, range=-2:10]
\begin{axis}[
  axis lines=middle,
  grid=major,
  xmin=-2,
  xmax=10,
  ymin=-2,
  ymax=20,
   y label style={at={(axis description cs:0.5,-0.05)},anchor=north},
    x label style={at={(axis description cs:-0.05,.5)},rotate=0,anchor=south},
  xlabel=$y$,
  ylabel=$x$,
  xtick={-2,-1,0,...,14},
  ytick={-2,0,...,20},
  tick style={very thick},
  legend style={
  at={(rel axis cs:0,1)},
  anchor=north west,draw=none,inner sep=0pt,fill=gray!10}
]
\addplot[black,thick,samples=1] {0};
\draw[color=red, smooth] plot (\x,\x*\x-8*\x+16) node[above left] {$y = 3$};
\draw (4,1.5) node[anchor=north]{(4,0)};
\draw (1.2,16) node[anchor=east]{(0,16)};
\draw (7,8)  node[anchor=west]{$y=x^2-8x+16$};
\end{axis}
\end{tikzpicture}  
\end{document}

当用pdflatex -shell-escape这个 png 文件进行编译时,会产生以下结果:

在此处输入图片描述

编辑:类型已修复,pgflatex-> pdflatex,感谢 quark67!

相关内容