答案1
如果您经常需要这样做,您可能希望采用更自动化的方法。
代码
\documentclass[varwidth]{standalone}
%\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, chains}
\newcommand*\matrixcomputation[2][]{%
\tikz[
start chain=going right, node distance=.5cm and 1cm,
every join/.append style={arrows={Latex[scale width=.7]-}},
n/.style={align=right, text depth=+0pt},#1]
\foreach[count=\i from 0, evaluate={\tw=max(width("$\a$"),width("$\b$"));}]
\a/\b in {#2} \path[text width=\tw]
node[n, on chain, join=by {path only, to path={(\tikzchainprevious.center)
to[bend left] node[above]{$x_{\i}$} (\tikzchaincurrent.center)}}] {$\a$}
\unless\ifnum\i=0 ([yshift=+.5ex]\tikzlastnode.south west)
edge[every join] ([yshift=+-.5ex]\tikzchainprevious'.north east)\fi
node[n, below=of \tikzchaincurrent](\tikzchaincurrent'){$\b$}
\unless\ifnum\i=0 ([yshift=+-.5ex]\tikzlastnode.north west)
edge[every join] ([yshift=+.5ex]\tikzchainprevious.south east)\fi;}
\begin{document}
\matrixcomputation{1/2, -1/-2, 3/1, 1/2}
\bigskip
\matrixcomputation{15/-2, 172/-3, 1/2, 5/10, 666/777}
\end{document}
输出
答案2
这可能需要进行一些调整,并且可能有更优雅的解决方案来移动标签:
\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\[\begin{tikzcd}[cells={nodes={text width=width("$-0$"), align=center}}]
1 \ar[rd] \ar[r, bend left, "x_1" above, phantom]
& -1 \ar[rd] \ar[r, bend left, "x_2" above, phantom]
& 3 \ar[rd] \ar[r, bend left, "x_3" above, phantom]
& 1
\\
2 \ar[ru]
& -2 \ar[ru]
& 1 \ar[ru]
& 2
\end{tikzcd}\]
\end{document}
答案3
与和 TikZ{NiceMatrix}
一起使用nicematrix
。
\documentclass{article}
\usepackage{nicematrix,tikz}
\begin{document}
\renewcommand{\arraystretch}{1.4}
\begin{NiceTabular}{*{7}{p[c]{5mm}}}
\RowStyle{\small}
& $x_1$ & & $x_2$ & & $x_3$ \\
$1$ & & $-1$ & & $3$ & & $1$ \\
$2$ & & $-2$ & & $1$ & & $2$
\CodeAfter
\begin{tikzpicture} [->, shorten > = 3pt, shorten < = 3pt]
\draw (2-1) -- (3-3) ;
\draw (3-1) -- (2-3) ;
\draw (2-3) -- (3-5) ;
\draw (3-3) -- (2-5) ;
\draw (2-5) -- (3-7) ;
\draw (3-5) -- (2-7) ;
\end{tikzpicture}
\end{NiceTabular}
\end{document}