我想创建一个货币兑换图,如节点中所示,用货币符号而不是文字表示。
这是原始代码
\documentclass[10pt,a4paper]{article}
\usepackage{tikz,tkz-berge}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\foreach \nodename/\x/\y in {GBP/0/4, USD/6/0, EUR/0/-4, MYR/-6/0}
\node (\nodename) at (\x,\y) [shape=circle, inner sep=2pt, draw, thick {\nodename};
\path[->,>=stealth,thick]
(GBP) edge [bend left=12] node[fill=white, pos=0.75] {1.14} (EUR)
(EUR) edge [bend left=12] node[fill=white, pos=0.75] {0.88} (GBP)
(GBP) edge [bend left=12] node[fill=white, pos=0.5] {5.35} (MYR)
(MYR) edge [bend left=12] node[fill=white, pos=0.5] {0.19} (GBP)
(GBP) edge [bend left=12] node[fill=white, pos=0.5] {1.34} (USD)
(USD) edge [bend left=12] node[fill=white, pos=0.5] {0.74} (GBP)
(EUR) edge [bend left=12] node[fill=white, pos=0.5] {1.18} (USD)
(USD) edge [bend left=12] node[fill=white, pos=0.5] {0.85} (EUR)
(EUR) edge [bend left=12] node[fill=white, pos=0.5] {4.71} (MYR)
(MYR) edge [bend left=12] node[fill=white, pos=0.5] {0.21} (EUR)
(USD) edge [bend left=10] node[fill=white, pos=0.7] {3.99} (MYR)
(MYR) edge [bend left=10] node[fill=white, pos=0.7] {0.25} (USD);
\end{tikzpicture}
\caption{A digraph showing exchange rates between US Dollar (USD), Ringgit Malaysia (MYR), Pounds (GBP), and Euro (EUR).}
\end{figure}
\end{document}
因此,我将代码中的所有 改为GBP
、\pounds
改为EUR
(\euro
改为\usepackage{eurosym}
)、MYR
改为RM
和USD
改为\$
,但它不起作用。
代码中有什么错误吗?谢谢。
答案1
当然可以,只是不能用作节点名称。您使用了\nodename
两次,一次是给节点命名。在那里您不能使用符号。而您使用的\nodename
是节点的内容,在那里符号是可以的。您需要做的就是解开这两者。
\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usepackage{eurosym}
\begin{document}
\begin{figure}
\begin{tikzpicture}[>=stealth]
\foreach \X/\x/\y/\Symb in
{GBP/0/4/\pounds,USD/6/0/\$,EUR/0/-4/\euro,MYR/{-6}/0/RM}
{\node (\X) at (\x,\y) [shape=circle, inner sep=2pt, draw, thick]
{\Symb};
}
\path[->,thick]
(GBP) edge [bend left=12] node[fill=white, pos=0.75] {1.14} (EUR)
(EUR) edge [bend left=12] node[fill=white, pos=0.75] {0.88} (GBP)
(GBP) edge [bend left=12] node[fill=white, pos=0.5] {5.35} (MYR)
(MYR) edge [bend left=12] node[fill=white, pos=0.5] {0.19} (GBP)
(GBP) edge [bend left=12] node[fill=white, pos=0.5] {1.34} (USD)
(USD) edge [bend left=12] node[fill=white, pos=0.5] {0.74} (GBP)
(EUR) edge [bend left=12] node[fill=white, pos=0.5] {1.18} (USD)
(USD) edge [bend left=12] node[fill=white, pos=0.5] {0.85} (EUR)
(EUR) edge [bend left=12] node[fill=white, pos=0.5] {4.71} (MYR)
(MYR) edge [bend left=12] node[fill=white, pos=0.5] {0.21} (EUR)
(USD) edge [bend left=10] node[fill=white, pos=0.7] {3.99} (MYR)
(MYR) edge [bend left=10] node[fill=white, pos=0.7] {0.25} (USD);
\end{tikzpicture}
\caption{A digraph showing exchange rates between US Dollar (\$),
Ringgit Malaysia (RM), Pounds (\pounds), and Euro (\euro).}
\end{figure}
\end{document}
答案2
绘制图表的另一种方法:不使用\foreach
循环,而使用edge quotes
。节点通过库的帮助进行定位positioning
:
\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{quotes,positioning}
\usepackage{eurosym}
\begin{document}
\begin{figure}
\begin{tikzpicture}[
node distance = 3 and 5,
every node/.style = {circle, draw, thick, minimum size=8mm, inner sep=2pt},
every edge/.style = {draw, thick, -stealth, bend left=12},
every edge quotes/.style={draw=none, fill=white}
]
\node (MYR) {RM};
\node (GBP) [above right=of MYR] {\pounds};
\node (USD) [below right=of GBP] {\$};
\node (EUR) [below right=of MYR] {\euro};
%
\draw (GBP) edge [pos=0.75, "1.14"] (EUR)
(EUR) edge [pos=0.75, "0.88"] (GBP)
(GBP) edge ["5.35"] (MYR)
(MYR) edge ["0.19"] (GBP)
(GBP) edge ["1.34"] (USD)
(USD) edge ["0.74"] (GBP)
(EUR) edge ["1.18"] (USD)
(USD) edge ["0.85"] (EUR)
(EUR) edge ["4.71"] (MYR)
(MYR) edge ["0.21"] (EUR)
(USD) edge [pos=0.7, "3.99"] (MYR)
(MYR) edge [pos=0.7, "0.25"] (USD);
\end{tikzpicture}
\caption{A digraph showing exchange rates between US Dollar (\$),
Ringgit Malaysia (RM), Pounds (\pounds), and Euro (\euro).}
\end{figure}
\end{document}