如何將algpseudocode
套件編寫的偽代碼放置在節點內tikz
? (我不想只裝飾程式碼,而是在圖表中使用它們)
微量元素
\documentclass{article}
\usepackage{tikz}
\usepackage{algpseudocode}
\begin{document}
\begin{tikzpicture}
\node[draw, rounded corners] {%
\begin{algorithmic}
\If{$\mathrm{n}=\mathrm{np}$}
\State Have fun
\EndIf
\end{algorithmic}%
};
\end{tikzpicture}
\end{document}
結果
很多錯誤,例如:
出了點問題——也許缺少\item。
答案1
環境algorithmic
無法放入盒子中,但您可以將其放入minipage
.由於您希望文字適合偽代碼片段的自然寬度,因此我使用了該varwidth
套件而不是minipage
直接使用 a。
\documentclass{article}
\usepackage{tikz}
\usepackage{varwidth}
\usepackage{algpseudocode}
\begin{document}
\begin{tikzpicture}
\node[draw, rounded corners] {%
\begin{varwidth}{\linewidth}
\begin{algorithmic}
\If{$\mathrm{n}=\mathrm{np}$}
\State Have fun
\EndIf
\end{algorithmic}%
\end{varwidth}
%
};
\end{tikzpicture}
\end{document}