我想从文本文件中读取数据并将其用作 Tikz 变量。我使用 datatool 在 overleaf 中导入数据,但无法在 Tikz 中使用它。我想从“automton.txt”中读取路径信息并将其用于 Tikz,这样\def\statedef{{2,0,2,5}}
我就想使用而不是\def\statedef{{\xcor{path1},\ycor{path1},\findx{path1},\lindx{path1}}}
。有什么想法吗?此外,由于文本文件可以包含未知数量的键,我想迭代文本文件并存储所有值,然后在 Tikz 中绘制它们。
\documentclass[tikz,border=1cm]{standalone}
\usepackage{graphicx} % Required for inserting images
\usepackage{amsthm}
\pdfoutput=1
\usepackage{multirow}
\usepackage{lettrine}
\usepackage{graphicx,float}
\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows.meta,calc,cd}
\usetikzlibrary{patterns}
\usetikzlibrary{datavisualization}
\tikzset{
->, % makes the edges directed
>=stealth, % makes the arrow heads bold
node distance=3cm, % specifies the minimum distance between two nodes. Change if necessary.
every state/.style={thick, fill=gray!10}, % sets the properties for each ’state’ node
initial text=$ $, % sets the text that appears on the start arrow
}
\usepackage[fleqn]{amsmath}
\usepackage{amssymb}
\usepackage{cite}
\usepackage{lettrine}
\let\labelindent\relax
\usepackage{enumitem}
\usepackage{multicol}
\usepackage{readarray}
\usepackage{pgfplots}
\usepackage{pgfmath}
\makeatletter
\newenvironment{customlegend}[1][]{%
\begingroup
% inits/clears the lists (which might be populated from previous
% axes):
\pgfplots@init@cleared@structures
\pgfplotsset{#1}%
}{%
% draws the legend:
\pgfplots@createlegend
\endgroup
}%
% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\pgfplots@addlegendimage}
\makeatother
\usepackage[T1]{fontenc}
\usepackage[export]{adjustbox}
\usepackage{booktabs,makecell}
\usepackage{datatool}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage[labelsep=quad,indention=10pt]{subfig}
\usepackage{ifthen}
\begin{document}
\tikzset{
redfill/.code={
\definecolor{mycolour1}{rgb}{1,0,0}%
\pgfkeysalso{/tikz/fill=mycolour1!40}
}
}
\tikzset{
greenfill/.code={
\definecolor{mycolour2}{rgb}{0,1,0}%
\pgfkeysalso{/tikz/fill=mycolour2!30}
}
}
\tikzset{
bluefill/.code={
\definecolor{mycolour3}{rgb}{0,0,1}%
\pgfkeysalso{/tikz/fill=mycolour3!40}
}
}
\tikzset{
cyanfill/.code={
\pgfkeysalso{/tikz/fill=teal!60}
}
}
\tikzset{
twocolorfill/.style n args={2}{path picture={
\fill[#1] (path picture bounding box.south)--(path picture bounding box.north)--(path picture bounding box.north east)--(path picture bounding box.south east)--cycle;
\fill[#2] (path picture bounding box.south)--(path picture bounding box.north)--(path picture bounding box.north west)--(path picture bounding box.south west)--cycle;
}}
}
\tikzset{
threecolorfill/.style n args={3}{path picture={
\fill[#1] (path picture bounding box.east)--(path picture bounding box.north east)--(path picture bounding box.north west)--(path picture bounding box.west)--cycle;
\fill[#2] (path picture bounding box.center) -- (path picture bounding box.south) arc [start angle=-90, delta angle=-90, radius=1] -- cycle;
\fill[#3] (path picture bounding box.center) -- (path picture bounding box.south) arc [start angle=-90, delta angle=90, radius=1] -- cycle;
}}
}
\tikzset{
fourcolorfill/.style n args={4}{path picture={
\fill[#1] (path picture bounding box.east)--(path picture bounding box.north east)--(path picture bounding box.north)--(path picture bounding box.center)--cycle;
\fill[#2] (path picture bounding box.center)--(path picture bounding box.north)--(path picture bounding box.north west)--(path picture bounding box.west)--cycle;
\fill[#3] (path picture bounding box.center)--(path picture bounding box.south)--(path picture bounding box.south west) -- (path picture bounding box.west) -- cycle;
\fill[#4] (path picture bounding box.center)--(path picture bounding box.south)--(path picture bounding box.south east) -- (path picture bounding box.east) -- cycle;
}}
}
\tikzset{
colorfill1_sub/.code=
{
\ifnum\pgfkeysvalueof{/tikz/colorfill_item}=#1
\tikzset{greenfill}
\else
\fi
},
colorfill2_sub/.code=
{
\ifnum\pgfkeysvalueof{/tikz/colorfill_item}=#1
\tikzset{redfill}
\else
\fi
},
colorfill3_sub/.code=
{
\ifnum\pgfkeysvalueof{/tikz/colorfill_item}=#1
\tikzset{bluefill}
\else
\fi
},
colorfill4_sub/.code=
{
\ifnum\pgfkeysvalueof{/tikz/colorfill_item}=#1
\tikzset{cyanfill}
\else
\fi
},
hashfill1_sub/.code=
{
\ifnum\pgfkeysvalueof{/tikz/colorfill_item}=#1
\tikzset{pattern=north west lines, pattern color=black}
\else
\fi
},
fourcolor1_sub/.code=
{
\ifnum\pgfkeysvalueof{/tikz/colorfill_item}=#1
\tikzset{fourcolorfill={cyanfill}{redfill}{greenfill}{blue!50}}
\else
\fi
},
threecolor1_sub/.code=
{
\ifnum\pgfkeysvalueof{/tikz/colorfill_item}=#1
\tikzset{threecolorfill={cyanfill}{greenfill}{blue!50}}
\else
\fi
},
twocolor1_sub/.code=
{
\ifnum\pgfkeysvalueof{/tikz/colorfill_item}=#1
\tikzset{twocolorfill={blue!50}{cyanfill}}
\else
\fi
},
colorfill_item/.initial=0,
colorfill/.code n args={9}
{
\tikzset{colorfill_item=#1,colorfill1_sub/.list=#2,colorfill2_sub/.list=#3,colorfill3_sub/.list=#4,colorfill4_sub/.list=#5,hashfill1_sub/.list=#6,fourcolor1_sub/.list=#7,threecolor1_sub/.list=#8,twocolor1_sub/.list=#9}
}
}
\begin{filecontents*}{automaton.txt}
path1,2,0,2,5
path2,2,2,6,8
path3,2,-2,9,11
\end{filecontents*}
\DTLloaddb[noheader,keys={thekey,v1,v2,v3,v4}]{data}{automaton.txt}
\DTLdisplaydb[noheader]{data}
\DTLgetvalue{\myb}{data}{2}{2}
\newcommand{\xcor}[1]{\DTLfetch{data}{thekey}{#1}{v1}}
\newcommand{\ycor}[1]{\DTLfetch{data}{thekey}{#1}{v2}}
\newcommand{\findx}[1]{\DTLfetch{data}{thekey}{#1}{v3}}
\newcommand{\lindx}[1]{\DTLfetch{data}{thekey}{#1}{v4}}
\xcor{path1}
\ycor{path1}
\findx{path1}
\lindx{path1}
\begin{figure*}[h]
\centering
\begin{adjustbox}{minipage=\textwidth,scale=0.8}
\begin{tikzpicture}[node distance = 2cm, on grid,>={Stealth[inset=0pt,length=6pt,angle'=28,round]}
]
\begin{customlegend}[legend style={at={(10,-8)},anchor=south},legend columns=-1,legend style={column sep=1ex},legend entries={Proposed Method,PCM PTA-MPC,CB PTA-MPC,PTA-MPC,Failed State}]
\addlegendimage{black,only marks, mark=*, cyanfill,style={scale=4}}
\addlegendimage{black,only marks, mark=*, bluefill,style={scale=4}}
\addlegendimage{black,only marks, mark=*, greenfill,style={scale=4}}
\addlegendimage{black,only marks, mark=*, redfill,style={scale=4}}
\addlegendimage{black,only marks, mark=*, pattern=north west lines, pattern color=black,style={scale=4}}
\end{customlegend};
%% DO NOT put extra space below!!!
%% {first state xcoor,first state ycoor,first state superscript, last state superscript}
%% State Definition
\def\statedef{{2,0,2,5}}
%% {first state superscript, last state superscript}
%% Straight Edge Definition
\def\straightsolidedge{{1,5}}
\def\bendrightsolidedge{}
\def\bendleftsolidedge{}
%% Color Definiton
\def\rstates{{5}}
\def\gstates{{}}
\def\bstates{{}}
\def\cstates{{3}}
\def\hashedstates{{}}
\def\bicolorstates{{3}}
\def\tricolorstates{{}}
\def\quadcolorstates{{4}}
\node[state,initial,fourcolorfill={cyanfill}{redfill}{greenfill}{blue!50}](q1){$q^{1}$};
\foreach \e in \statedef
{\edef\mye{{\e}}
\pgfmathsetmacro{\myx}{\mye[0]}
\pgfmathsetmacro{\myy}{\mye[1]}
\pgfmathsetmacro{\myfirstind}{\mye[2]}
\pgfmathsetmacro{\mystartind}{int(\mye[2]+1)}
\pgfmathsetmacro{\mylastind}{\mye[3]}
\node [state, colorfill/.expanded={\myfirstind}{\gstates}{\rstates}{\bstates}{\cstates}{\hashedstates}{\quadcolorstates}{\tricolorstates}{\bicolorstates}] at (\myx,\myy) (q\myfirstind){$q^{\myfirstind}$};
\foreach \a[evaluate=\a as \b using int(\a-1)] in {\mystartind,...,\mylastind}{
\node[state, right of=q\b, colorfill/.expanded={\a}{\gstates}{\rstates}{\bstates}{\cstates}{\hashedstates}{\quadcolorstates}{\tricolorstates}{\bicolorstates}] (q\a){$q^{\a}$};
}
}
\foreach \r in \straightsolidedge
{\edef\myr{{\r}}
\pgfmathsetmacro{\mystartstate}{\myr[0]}
\pgfmathsetmacro{\myendstate}{\myr[1]-1}
\foreach \d[evaluate=\d as \e using int(\d+1)] in {\mystartstate,...,\myendstate}{
\draw (q\d) edge (q\e);
}
}
\foreach \t in \bendrightsolidedge
{\edef\myt{{\t}}
\pgfmathsetmacro{\f}{\myt[0]}
\pgfmathsetmacro{\g}{\myt[1]}
\draw (q\f) edge[bend right] (q\g);
}
\foreach \u in \bendleftsolidedge
{\edef\myu{{\u}}
\pgfmathsetmacro{\h}{\myu[0]}
\pgfmathsetmacro{\i}{\myu[1]}
\draw (q\h) edge[bend left] (q\i);
}
\end{tikzpicture}
\end{adjustbox}
\end{figure*}
\end{document}