% Updated graphviz.sty stuff ----------------------------------------------------------------------------------
% Original source at https://github.com/mprentice/GraphViz-sty
\RequirePackage{verbatim}

\newcounter{graphvizFileCounter}
\newcommand\graphvizPrefix{gvAutoFile}
\newcommand\graphvizGetName{\graphvizPrefix\arabic{graphvizFileCounter}}
\newwrite\graphvizFile
\newcommand\graphvizDefaultIncludegraphicsOptions{}

 % 1: (optional) options for includegraphics
% 2: layout engine (the -K argument for dot)
% 3: complete graph data
\newcommand\graphvizBase[3][\graphvizDefaultIncludegraphicsOptions]{% (includegraphics opts, layout engine, graph)
	\immediate\openout\graphvizFile=\graphvizGetName.dot
	\immediate\write\graphvizFile{\string#3}
	\immediate\closeout\graphvizFile
	\immediate\write18{bash -c "dot -K#2 -Tpdf -Gpad=0 -Gmargin=0 -Gbgcolor=transparent \graphvizGetName.dot > \graphvizGetName.pdf 2> \graphvizGetName.log && echo NoLog > \graphvizGetName.log" || rm \graphvizGetName.pdf}
	\IfFileExists{\graphvizGetName.pdf}
	{\includegraphics[#1]{\graphvizGetName}}% the pdf exists: include it 
	{% the pdf was not created - show a hint and std error from dot
		\fbox{\begin{minipage}{\textwidth}
			Error in Graphviz: the file was not compiled. Remeber to use \texttt{pdflatex} with the \texttt{-shell-escape} option.\\
			Dot log (might be empty):
			\verbatiminput{\graphvizGetName.log}
		\end{minipage}}

	}
	\addtocounter{graphvizFileCounter}{1}% select next name
}

%\newcommand\graphvizBase[3][\graphvizDefaultIncludegraphicsOptions]{% (includegraphics opts, layout engine, graph)
%	\immediate\openout\graphvizFile=\graphvizGetName.dot
%	\immediate\write\graphvizFile{\string#3}
%	\immediate\closeout\graphvizFile
%	\immediate\write18{bash -c "dot2tex --usepdflatex --prog #2 --figonly --tikzedgelabels -s -t math \graphvizGetName.dot > \graphvizGetName.tex 2> \graphvizGetName.log && echo NoLog > \graphvizGetName.log" || rm \graphvizGetName.tex}
%	\IfFileExists{\graphvizGetName.tex}
%	{\input{\graphvizGetName}}% the tex exists: include it 
%	{% the tex was not created - show a hint and std error from dot
%		\fbox{\begin{minipage}{\textwidth}
%			Error in Graphviz: the file was not compiled. Remeber to use \texttt{pdflatex} with the \texttt{-shell-escape} option.\\
%			Dot log (might be empty):
%			\verbatiminput{\graphvizGetName.log}
%		\end{minipage}}
%
%	}
%	\addtocounter{graphvizFileCounter}{1}% select next name
%}

 % 1: (optional) options for includegraphics
% 2: graph data, without digraph declaration
\newcommand\neatoDigraph[2][\graphvizDefaultIncludegraphicsOptions]{
	\graphvizBase[#1]{neato}{digraph {#2}}
}
 % 1: (optional) options for includegraphics
% 2: graph data, without graph declaration
\newcommand\neatoGraph[2][\graphvizDefaultIncludegraphicsOptions]{
	\graphvizBase[#1]{neato}{graph {#2}}
}
 % 1: (optional) options for includegraphics
% 2: graph data, without digraph declaration
\newcommand\dotDigraph[2][\graphvizDefaultIncludegraphicsOptions]{
	\graphvizBase[#1]{dot}{digraph {#2}}
}
 % 1: (optional) options for includegraphics
% 2: graph data, without graph declaration
\newcommand\dotGraph[2][\graphvizDefaultIncludegraphicsOptions]{
	\graphvizBase[#1]{dot}{graph {#2}}
}
% Updated graphviz.sty stuff end ----------------------------------------------------------------------------
% Obabel inclusion ------------------------------------------------------------------------------------------------
\newcommand\obabel[2][\graphvizDefaultIncludegraphicsOptions]{
	\immediate\write18{obabel -:"#2" -O \graphvizGetName.svg 2> \graphvizGetName.log && inkscape -f \graphvizGetName.svg --export-pdf=\graphvizGetName.pdf 2>> \graphvizGetName.log || rm -f \graphvizGetName.pdf}
	\IfFileExists{\graphvizGetName.pdf}
	{\includegraphics[#1]{\graphvizGetName}}% the pdf exists: include it 
	{% the pdf was not created - show a hint and std error from dot
		\fbox{\begin{minipage}{\textwidth}
			Error in obabel: the file was not compiled. Remeber to use \texttt{pdflatex} with the \texttt{-shell-escape} option.\\
			obabel log (might be empty):
			\verbatiminput{\graphvizGetName.log}
		\end{minipage}}
	}
	\addtocounter{graphvizFileCounter}{1}% select next name
}
% end -------------------------------------------------------------------------------------------------------------------

