6

Can any one tell me how to make a figure exactly like this picture, with 2 arrows etc.

this picture

This is my unsuccessful attempt:

\begin{figure}[ht]
\centering
\begin{minipage}[b]{0.35\linewidth}
\begin{lstlisting}[language=C, frame=single,
xleftmargin=5.0ex,basicstyle=\footnotesize\ttfamily,
numbers=left,stepnumber=0, morekeywords={assert} ]
x=x+y;
if(x!=1){
  x=2; 
  if(z) x++; 
}
\end{lstlisting}
\end{minipage}
\begin{minipage}[b]{0.35\linewidth}
\begin{lstlisting}[mathescape % to use mathmode inside listing
,language=C, frame=single,
xleftmargin=5.0ex,basicstyle=\footnotesize\ttfamily,
numbers=left,stepnumber=0, morekeywords={assert} ]
x$_1$=x$_0$+y$_0$;
if(x$_1$!=1){
  x$_2$=2; 
  if(z$_0$) x$_3$=x$_2$+1; 
}
\end{lstlisting}
\end{minipage}
\fbox{
\begin{minipage}[b]{0.4\linewidth}
\begin{align*}
x_1 &= x_0 + y_0 \wedge
\\ x_2 &= ((x_1 \neq 1)?2:x_1) \wedge
\\ x_3 &= ((x_1 \neq 1 \wedge z_0)?x_2+1:x_2) 
\end{align*}
\end{minipage}
}
\end{figure}

Thank you.

  • Please can you name the "THIS"(very vague in word search) figure in Question title as something like equations in box/code snippets in box or any known technical tile for easy searchability of future Users – texenthusiast Oct 20 '13 at 15:16
  • Thanks for your comment. I edited, could you please check if the title is OK now? – qsp Oct 20 '13 at 15:25
  • Thanks for your edit and It looks good,atleast better than "this" :) – texenthusiast Oct 20 '13 at 15:28
6

The best would be using an align* environment, but lstlistings cannot be used; here's a workaround.

\documentclass{article}
\usepackage{amsmath,listings}

\newcommand{\mt}[1]{\mathtt{#1}} % for typewriter subscript in listings

\lstnewenvironment{qsplisting}[1][] % avoid specifying the same thing several times
 {\lstset{
    mathescape, % to use mathmode in side listing
    language=C,
    frame=single,
    basicstyle=\footnotesize\ttfamily,
    numbers=left,
    stepnumber=0,
    morekeywords={assert},
    #1
  }}{}

\newsavebox{\leftlisting}
\newsavebox{\rightlisting}

\begin{document}
\begin{figure}[htp]
\begin{lrbox}{\leftlisting}
\begin{minipage}{0.2\linewidth}
\begin{qsplisting}
x=x+y;
if(x!=1){
  x=2; 
  if(z) x++; 
}
\end{qsplisting}
\end{minipage}
\end{lrbox}
\begin{lrbox}{\rightlisting}
\begin{minipage}{0.25\linewidth}
\begin{qsplisting}
x$_\mt{1}$=x$_\mt{0}$+y$_\mt{0}$;
if(x$_\mt{1}$!=1){
  x$_\mt{2}$=2; 
  if(z$_\mt{0}$) x$_\mt{3}$=x$_\mt{2}$+1; 
}
\end{qsplisting}
\end{minipage}
\end{lrbox}

\begin{align*}
& \usebox{\leftlisting} \quad\to\quad \usebox{\rightlisting} \\
&\to\boxed{
\begin{aligned}
\mathcal{C}&:= 
  \!\begin{aligned}[t]
  x_1 &= x_0 + y_0 \wedge{} \\ 
  x_2 &= ((x_1 \neq 1)?2:x_1) \wedge{} \\ 
  x_3 &= ((x_1 \neq 1 \wedge z_0)?x_2+1:x_2)
  \end{aligned}
\\
\mathcal{P}&:= x_3\le 3
\end{aligned}
}
\end{align*}
\end{figure}
\end{document}

Note: always add the p positioning specifier for figures

enter image description here

| improve this answer | |
  • I know I should avoid "thank" comment. But this is really what I need, thank you very much :) – qsp Oct 20 '13 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.