0

I need to create and save latex drawing so i can use there else where.

Im using the code

\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}  

and i want to save the image it creates as a png. I need to create multiple of these with differences to each image. So i need to create all the images with the exact scale and size so doing a screenshot of the page is not good enough.

From the comments I have developed the code including the preamble from my original document.

\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}

This creates my graph but not in the centre of the page and the graph goes over the page.

enter image description here

Using the code given below for the equation when it works perfectly. However if i try and do another graph it goes over the page. enter image description here

  • 1
    Just compile it as a standalone document? – user121799 Apr 5 at 20:49
  • wouldnt that create a A4 page? I want it to be the size of the graph – Jettie Baker Apr 5 at 20:50
  • 2
    No, it wouldn't. Id you use \documentclass[tikz]{standalone} this will produce a pdf of the size of the tikz graph. If you use the convert options of standalone appropriately, you can create automatically a png of the same page size. – user121799 Apr 5 at 20:53
  • I dont understand how to do what you are trying to say – Jettie Baker Apr 5 at 20:58
  • Marmot's solution generates a pdf file with your diagram. You can use another tool to convert the pdf into another format, like jpg or png. This has nothing to do with latex directly. Examples for tools are ghostscript or ImageMagick. – Dr. Manuel Kuehner Apr 8 at 6:24
3

I'll be happy to remove this but if I compile

\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}

with pdflatex -shell-escape I get a png file of the right size.

Thanks for adding a preamble. Unfortunately, your code throws errors and, related to it, the preamble is by far not minimal. After stripping off all things that are not related to the tikzpicture one arrives at

\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}

which produces, when compiled with pdflatex -shell-escape this very png file:

enter image description here

EDIT: Type fixed, pgflatex -> pdflatex, thanks to quark67!

  • 2
    @JettieBaker I think marmot used your code directly, just adding the preamble and document environment, so the output is as you specified it. That said, expect for the fact that the node with $y = 3$ is clipped away as it's far outside the axis boundaries, I don't immediately see what is wrong with the output, you might have to be more specific. – Torbjørn T. Apr 5 at 22:07
  • 1
    No certainly nothing to do with the conversion, I was just speculating about what Jettie thought was wrong with the diagram. – Torbjørn T. Apr 5 at 23:13
  • 1
    @JettieBaker If the output of the above is different from the output you expect, this is most likely due to some directives you have in your document. You do not provide us with this preamble. If you want to find out what is going on, please provide us with a full MWE, i.e. a document that starts with \documentclass and ends with \end{document} and can be compiled. – user121799 Apr 6 at 17:23
  • 2
    @JettieBaker Well, unfortunately your code throws errors. I stripped off parts of your code that are not needed and/or cause errors, and posted the code and result in my answer. And yes, when I compile the file with pdflatex -shell-escape, it does produce a png on my machine. – user121799 Apr 7 at 20:44
  • 1
    @JettieBaker You can convert pdf's to png with convert, which comes with gimp AFAIK. – user121799 Apr 8 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.