
我有以下程式碼,如圖像下方所示。我現在的輸出就像下面的第一張圖。
我希望輸出如下圖所示,中間有藍色和紅色箭頭。我似乎無法更改箭頭的顏色和位置。
代碼如下。誰能幫我嗎?
\begin{tabular}{|r|r|r|r|r|r|}
\hline
V & M & V & M & V & M \\ \hline
6 & 4 & NaN & NaN & NaN & NaN \\ \hline
7 & 3 & 5 & 5 \tikzmark{f} & NaN & NaN \\ \hline
NaN & NaN & 8 & \tikzmark{d}{2} \tikzmark{e} & \tikzmark{c}{4} & \tikzmark{b}{6} \\ \hline
NaN & NaN & NaN & NaN & 10 & \tikzmark{a}{0} \\ \hline
\end{tabular}
\begin{tikzpicture}[overlay, remember picture, shorten >=.5pt, shorten <=.5pt]
\draw [->] ({pic cs:a}) [line width=0.35mm, yshift=-1] to ({pic cs:b});
\draw [->] ({pic cs:c}) [line width=0.35mm, yshift=-1] to ({pic cs:d});
\draw [->] ({pic cs:e}) [line width=0.35mm, yshift=-1] to ({pic cs:f});
\end{tikzpicture}
答案1
\tikzmark
我不會使用 a matrix of math nodes
inside a tikz ,而是使用a inside a tikz \matrix
。當您在表格環境中繪製條目時,請變更為使用 amatrix of math nodes
實際上簡化了程式碼。
我將給出輸出、程式碼,然後解釋它是如何運作的。首先,輸出:
接下來是程式碼:
\documentclass[border=5mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[>=stealth,->,shorten >=2pt,looseness=.5,auto]
\matrix (M)[matrix of math nodes,row sep=0mm,column sep=0mm,
nodes=draw, minimum size=12mm]{
V & M & V & M & V & M \\
6 & 4 & NaN & NaN & NaN & NaN \\
7 & 3 & 5 & 5 & NaN & NaN \\
NaN & NaN & 8 & 2 & 4 & 6 \\
NaN & NaN & NaN & NaN & 10 & 0 \\
};
\draw[ultra thick,blue,->] (M-5-6.west) -- (M-4-6.west);
\draw[ultra thick,red,->] (M-5-5.north) -- (M-5-4.north);
\draw[ultra thick,blue,->] (M-4-4.west) -- (M-3-4.west);
\draw[ultra thick,red,->] (M-4-3.north) -- (M-4-2.north);
\draw[ultra thick,blue,->] (M-3-2.west) -- (M-2-2.west);
\end{tikzpicture}
\end{document}
現在解釋一下:
此 \matrix (M)
指令設定一個矩陣,其座標由 指定(M-<row>-<col>)
,其中<row>
和<col>
是行索引和列索引。如果您改寫,\matrix (mat)
那麼您會將座標稱為(mat-<row>-<col>)
。
接下來,您可以使用north
、south
、west
等north west
來調整節點規格所指的是矩陣單元的哪一部分。例如,(M-4-3.west)
指的是第 4 行第 3 列節點的西邊。
在matrix of math nodes
我已經設定了row sep=0mm,column sep=0mm, nodes=draw, minimum size=12mm
。這是為了繪製網格:行和列之間沒有間隙,每個節點周圍應該繪製一個矩形,並且最小單元格大小為12m
選擇最小單元格大小以適合OP單元格的內容。就我個人而言,我不會繪製網格(書本標籤包解釋了為什麼你應該避免表格中的垂直線),但這正是問題所要求的,所以...
如果您希望佈局與 MWE 中的佈局相同,請使用矩形單元而不是程式碼中的方形單元,則將 變更minimum size=12mm
為minimum width=12mm
.為了顯示它的樣子,並進一步突出顯示節點規格,如果將繪製語句更改為
\draw[ultra thick,blue,->] (M-5-6.south west) -- (M-4-6.south west);
\draw[ultra thick,red,->] (M-5-5.north east) -- (M-5-4.north west);
\draw[ultra thick,blue,->] (M-4-4.south west) -- (M-3-4.south west);
\draw[ultra thick,red,->] (M-4-3.north east) -- (M-4-2.north west);
\draw[ultra thick,blue,->] (M-3-2.south west) -- (M-2-2.south west);
然後minimum size=12mm
你得到
編輯(回答評論裡的問題)
若要對列進行著色,只需將以下內容every odd column
和every even column
樣式新增至矩陣選項即可:
every odd column/.style={nodes={fill=pink}},
every even column/.style={nodes={fill=blue!20!white}},
完成這些後,您的圖片將變為:
我相信,您看到的“雙”線是渲染錯誤,並且可能依賴瀏覽器,並且在列印時它們可能不會出現(我不知道,我沒有檢查)。對於上面的圖片,我以非常快的速度消除了它們特別指定透過將行和列分隔更改為:
row sep=-0.1mm,column sep=-0.1mm