如何創建從一個數字到矩陣上方和下方的另一個數字的彎曲箭頭?

如何創建從一個數字到矩陣上方和下方的另一個數字的彎曲箭頭?

我的程式碼:

\documentclass[12pt]{article}
\usepackage{amsmath}

\begin{document}

\begin{align}
   \pi=\begin{pmatrix}
        1 & 2 & 3 & 4 & 5 & 6 & 7 & 8\\
        1 & 7 & 5 & 2 & 4 & 6 & 3 & 8
    \end{pmatrix}
\end{align}

\end{document}

在此輸入影像描述

如何創建這樣的箭頭?從一個數字到另一個數字,上面/下面有文字並著色。

答案1

我建議使用tikzmark.請記住,您必須編譯兩次。

無關:您似乎沒有使用align,所以我將其更改為equation

在此輸入影像描述

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}

\begin{equation}
   \pi=\begin{pmatrix}
        1 & \tikzmarknode[blue!70!black]{a}{2} & 3 & \tikzmarknode[green!50!black]{b}{4} & 5 & 6 & 7 & 8\\
        1 & \tikzmarknode[blue]{c}{7} & 5 & \tikzmarknode[green]{d}{2} & 4 & 6 & 3 & 8
    \end{pmatrix}
\end{equation}

\begin{tikzpicture}[remember picture,overlay]
\draw[-latex]([yshift=.5ex]a.north) to[bend left]node[above]{\scriptsize$\textcolor{blue!70!black}{i}<\textcolor{green!50!black}{j}$} ([yshift=.5ex]b.north);
\draw[-latex]([yshift=-.5ex]d.south) to[bend left]node[below]{\scriptsize$\textcolor{blue}{\pi(i)}>\textcolor{green}{\pi(j)}$} ([yshift=-.5ex]c.south);
\end{tikzpicture}

\end{document}

答案2

我提出一個pstricks解決方案:

    \documentclass[12pt]{article}
    \usepackage{amsmath}
    \usepackage[svgnames]{xcolor}
    \usepackage{pst-node}

    \begin{document}

    \begin{equation}
       \pi=\begin{pmatrix}
            1 & \rnode[t]{a}{\color{SteelBlue}2} & 3 & \rnode[t]{b}{\color{DarkSeaGreen}4} & 5 & 6 & 7 & 8\\
            1 & \rnode[b]{c}{\color{DeepSkyBlue}7} & 5 & \rnode[b]{d}{\color{SpringGreen}2} & 4 & 6 & 3 & 8
        \end{pmatrix}
    \psset{arrowinset=0.1, arrows=->, nodesep=1pt, arcangle=30}
    \ncarc{a}{b}\naput[labelsep=1pt]{{\color{SteelBlue}i }< {\color{DarkSeaGreen}j}}
    \ncarc{d}{c}\naput[labelsep=1pt]{{\color{DeepSkyBlue}\pi(i )} > {\color{SpringGreen}\pi( j)}}
    \end{equation}

    \end{document} 

在此輸入影像描述

答案3

使用{pNiceMatrix}nicematrix在矩陣中創建 PGF/Tikz 節點,Tikz 使用這些節點繪製箭頭。

\documentclass[12pt]{article}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{equation}
\pi =
\begin{pNiceMatrix}
     1 & 2 & 3 & 4 & 5 & 6 & 7 & 8\\
     1 & 7 & 5 & 2 & 4 & 6 & 3 & 8
\CodeAfter
     \begin{tikzpicture}
     \draw [->] (1-|2.5) to [bend left] node [above] {\scriptsize $i<j$} (1-|4.5);
     \draw [->] (3-|4.5) to [bend left] node[below] {\scriptsize$\pi(i)>\pi(j)$} (3-|2.5);
     \end{tikzpicture}
\end{pNiceMatrix}
\end{equation}

\end{document}

您需要多次編譯(因為 PGF/Tikz 節點)。

上述程式碼的輸出

相關內容