我把一个算法和一张图片并排放在一起。到目前为止,我意识到,通过将它们放在 minipage 环境中(算法和图片各占 0.5\textwidth)
这是一个最小的工作示例:
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{graphicx}
\usepackage{color}
\begin{document}
\begin{minipage}{.5\textwidth}
\begin{algorithm}[H]
\caption{\\ Stress calculation}
\begin{algorithmic}[1]
\State The first line of an longer algorithm
\end{algorithmic}
\end{algorithm}
\end{minipage}
\begin{minipage}{.5\textwidth}
\def\svgwidth{300pt}
\input{./example.pdf_tex}
\end{minipage}
\end{document}
文件example.pdf_tex是Inkscape生成的图片:
%%
\begingroup%
\makeatletter%
\providecommand\color[2][]{%
\errmessage{(Inkscape) Color is used for the text in Inkscape, but the package 'color.sty' is not loaded}%
\renewcommand\color[2][]{}%
}%
\providecommand\transparent[1]{%
\errmessage{(Inkscape) Transparency is used (non-zero) for the text in Inkscape, but the package 'transparent.sty' is not loaded}%
\renewcommand\transparent[1]{}%
}%
\providecommand\rotatebox[2]{#2}%
\newcommand*\fsize{\dimexpr\f@size pt\relax}%
\newcommand*\lineheight[1]{\fontsize{\fsize}{#1\fsize}\selectfont}%
\ifx\svgwidth\undefined%
\setlength{\unitlength}{156.0915483bp}%
\ifx\svgscale\undefined%
\relax%
\else%
\setlength{\unitlength}{\unitlength * \real{\svgscale}}%
\fi%
\else%
\setlength{\unitlength}{\svgwidth}%
\fi%
\global\let\svgwidth\undefined%
\global\let\svgscale\undefined%
\makeatother%
\begin{picture}(1,0.81532061)%
\lineheight{1}%
\setlength\tabcolsep{0pt}%
\put(0,0){\includegraphics[width=\unitlength,page=1]{example.pdf}}%
\put(1,0.31527883){\color[rgb]{0,0,0}\makebox(0,0)[lt]{\lineheight{1.25}\smash{\begin{tabular}[t]{l} \end{tabular}}}}%
\end{picture}%
\endgroup%
我想在算法和图片之间画一条线,但是 minipage 环境不允许离开图片的 0.5\textwidth 部分(如果我在 inkscape 中画这条线)。
我怎样才能做到这一点?
答案1
算法环境是浮点数,你不能将浮点数放在小页面中。能使用 paracol。
我在算法和图片环境中添加了 tikzmarks,然后用箭头将它们连接起来。这需要运行两次。
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{graphicx}
\usepackage{color}
\usepackage{paracol}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}
\begin{paracol}{2}
\begin{algorithm}[t]
\caption{\\ Stress calculation}
\begin{algorithmic}[1]
\State The first line of an longer algorithm \tikzmark{A}
\State The second line of an longer algorithm \tikzmark{B}
\end{algorithmic}
\end{algorithm}
\switchcolumn
\def\svgwidth{\columnwidth}
\begingroup%
\makeatletter%
\providecommand\color[2][]{%
\errmessage{(Inkscape) Color is used for the text in Inkscape, but the package 'color.sty' is not loaded}%
\renewcommand\color[2][]{}%
}%
\providecommand\transparent[1]{%
\errmessage{(Inkscape) Transparency is used (non-zero) for the text in Inkscape, but the package 'transparent.sty' is not loaded}%
\renewcommand\transparent[1]{}%
}%
\providecommand\rotatebox[2]{#2}%
\newcommand*\fsize{\dimexpr\f@size pt\relax}%
\newcommand*\lineheight[1]{\fontsize{\fsize}{#1\fsize}\selectfont}%
\ifx\svgwidth\undefined%
\setlength{\unitlength}{156.0915483bp}%
\ifx\svgscale\undefined%
\relax%
\else%
\setlength{\unitlength}{\unitlength * \real{\svgscale}}%
\fi%
\else%
\setlength{\unitlength}{\svgwidth}%
\fi%
\global\let\svgwidth\undefined%
\global\let\svgscale\undefined%
\makeatother%
\begin{picture}(1,0.81532061)%
\lineheight{1}%
\setlength\tabcolsep{0pt}%
\put(0,0.81532061) {\tikzmark{C}}% top of picture environment
\put(0,0) {\tikzmark{D}}% bottom of picture environment
\put(0,0){\includegraphics[width=\unitlength,page=1]{example-image}}%
\put(1,0.31527883){\color[rgb]{0,0,0}\makebox(0,0)[lt]{\lineheight{1.25}\smash{\begin{tabular}[t]{l} \end{tabular}}}}%
\end{picture}%
\endgroup%
\end{paracol}
\begin{tikzpicture}[overlay, remember picture]
\draw[red, <-] (pic cs:A) -- (pic cs:C);
\draw[blue, <-] (pic cs:B) -- (pic cs:D);
\end{tikzpicture}
\end{document}