6

I have the following code that produce a 3x3 grid with an arrow in it. Some of the grid cells have a # in them.

\begin{picture}(185,90)
\put(0,0){\framebox(30,30)}
\put(30,0){\framebox(30,30)}
\put(60,0){\framebox(30,30)}
\put(0,30){\framebox(30,30)}
\put(30,30){\framebox(30,30)}
\put(60,30){\framebox(30,30){\Large{\#}}}
\put(0,60){\framebox(30,30){\Large{\#}}}
\put(30,60){\framebox(30,30){\Large{\#}}}
\put(60,60){\framebox(30,30){\Large{\#}}}
\put(15,45){\vector(1,0){30}}
\end{picture}

Result produce with pdflatex

I would like to put a caption under the drawn picture. How could I achieve this? I believe that you can't put a caption inside a \begin{picture}. I have also try to wrap it inside a \begin{figure} without success.

  • This is not directly related to your actual question, but I would recommend you use tikz for drawing. – Peter Grill Apr 22 '15 at 19:26
  • @PeterGrill I will try to have a look at it soon. Thx for the suggestion – K-Jtan Apr 22 '15 at 19:33
3

Put the picture inside a minipage. This will allow it to be included in a figure environment, which will therefore allow a \caption.

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{figure}[ht]
\centering
\begin{minipage}{1.3in}
\begin{picture}(185,90)
\put(0,0){\framebox(30,30)}
\put(30,0){\framebox(30,30)}
\put(60,0){\framebox(30,30)}
\put(0,30){\framebox(30,30)}
\put(30,30){\framebox(30,30)}
\put(60,30){\framebox(30,30){\Large{\#}}}
\put(0,60){\framebox(30,30){\Large{\#}}}
\put(30,60){\framebox(30,30){\Large{\#}}}
\put(60,60){\framebox(30,30){\Large{\#}}}
\put(15,45){\vector(1,0){30}}
\end{picture}
\end{minipage}
\caption{Here is my caption.}
\end{figure}
\lipsum[2]
\end{document}

enter image description here

Following up on the OP's comment, one can, in order to get multiple images on a single line, place adjacent minipages on the same line, as long as they don't exceed the \linewidth.

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{figure}[ht]
\centering
\begin{minipage}{1.3in}
\begin{picture}(185,90)
\put(0,0){\framebox(30,30)}
\put(30,0){\framebox(30,30)}
\put(60,0){\framebox(30,30)}
\put(0,30){\framebox(30,30)}
\put(30,30){\framebox(30,30)}
\put(60,30){\framebox(30,30){\Large{\#}}}
\put(0,60){\framebox(30,30){\Large{\#}}}
\put(30,60){\framebox(30,30){\Large{\#}}}
\put(60,60){\framebox(30,30){\Large{\#}}}
\put(15,45){\vector(1,0){30}}
\end{picture}
\end{minipage}
\begin{minipage}{1.3in}
\begin{picture}(185,90)
\put(0,0){\framebox(30,30)}
\put(30,0){\framebox(30,30)}
\put(60,0){\framebox(30,30)}
\put(0,30){\framebox(30,30)}
\put(30,30){\framebox(30,30)}
\put(60,30){\framebox(30,30){\Large{\#}}}
\put(0,60){\framebox(30,30){\Large{\#}}}
\put(30,60){\framebox(30,30){\Large{\#}}}
\put(60,60){\framebox(30,30){\Large{\#}}}
\put(15,45){\vector(1,0){30}}
\end{picture}
\end{minipage}
\caption{Here is my caption.}
\end{figure}
\lipsum[2]
\end{document}

enter image description here

  • Thank you, I can work from that (need to find a way to place more than one on the same line but I'll try to find the solution on my own using this documentation : en.wikibooks.org/wiki/LaTeX/…) – K-Jtan Apr 22 '15 at 19:06
  • @K-Jtan You can just end one minipage and begin the next (with a new image), as long as the linewidth is not exceeded. – Steven B. Segletes Apr 22 '15 at 19:08
  • @K-Jtan See the 2nd example, in my revised answer. – Steven B. Segletes Apr 22 '15 at 19:10
  • I would need a caption for each minipage (something like Case 1, Case 2, ... Case n) – K-Jtan Apr 22 '15 at 19:31
  • @K-Jtan With the basic figure environment, LaTeX does not allow multiple "floats" on a single line. However, depending on your application, there is the subcaption package, which you may be able to adapt to your need. I would suggest searching this site for subcaption to find many examples of how it is used. – Steven B. Segletes Apr 22 '15 at 19:43
3

You're stating too big a bounding box for the picture; moreover you're forgetting the empty argument for the empty boxes. Note that \Large doesn't take an argument.

\documentclass{article}
\usepackage{pict2e}

\begin{document}

\begin{figure}
\centering

\begin{picture}(90,90)
\put(0,0){\framebox(30,30){}}
\put(30,0){\framebox(30,30){}}
\put(60,0){\framebox(30,30){}}
\put(0,30){\framebox(30,30){}}
\put(30,30){\framebox(30,30){}}
\put(60,30){\framebox(30,30){\Large\#}}
\put(0,60){\framebox(30,30){\Large\#}}
\put(30,60){\framebox(30,30){\Large\#}}
\put(60,60){\framebox(30,30){\Large\#}}
\put(15,45){\vector(1,0){30}}
\end{picture}

\caption{A picture}

\end{figure}

\end{document}

enter image description here

There is no problem with more than one picture:

\documentclass{article}
\usepackage{pict2e}

\begin{document}

\begin{figure}
\centering

\begin{picture}(90,90)
\put(0,0){\framebox(30,30){}}
\put(30,0){\framebox(30,30){}}
\put(60,0){\framebox(30,30){}}
\put(0,30){\framebox(30,30){}}
\put(30,30){\framebox(30,30){}}
\put(60,30){\framebox(30,30){\Large\#}}
\put(0,60){\framebox(30,30){\Large\#}}
\put(30,60){\framebox(30,30){\Large\#}}
\put(60,60){\framebox(30,30){\Large\#}}
\put(15,45){\vector(1,0){30}}
\end{picture}\qquad
\begin{picture}(90,90)
\put(0,0){\framebox(30,30){}}
\put(30,0){\framebox(30,30){}}
\put(60,0){\framebox(30,30){}}
\put(0,30){\framebox(30,30){}}
\put(30,30){\framebox(30,30){}}
\put(60,30){\framebox(30,30){\Large\#}}
\put(0,60){\framebox(30,30){\Large\#}}
\put(30,60){\framebox(30,30){\Large\#}}
\put(60,60){\framebox(30,30){\Large\#}}
\put(15,45){\vector(1,0){30}}
\end{picture}

\caption{A picture}

\end{figure}

\end{document}

enter image description here


A better way to draw the picture is using lines instead of boxes: this avoids the “doubling” of the inner lines.

\documentclass{article}
\usepackage{pict2e}

\begin{document}

\begin{figure}
\centering

\begin{picture}(90,90)
\polygon(0,0)(90,0)(90,90)(0,90)
\put(30,0){\line(0,1){90}}
\put(60,0){\line(0,1){90}}
\put(0,30){\line(1,0){90}}
\put(0,60){\line(1,0){90}}
\put(15,75){\makebox(0,0){\Large\#}}
\put(45,75){\makebox(0,0){\Large\#}}
\put(75,75){\makebox(0,0){\Large\#}}
\put(75,45){\makebox(0,0){\Large\#}}
\put(15,45){\vector(1,0){30}}
\end{picture}

\caption{A picture}

\end{figure}

\end{document}

Note how the border is drawn with \polygon.

enter image description here

1

The question is quite old, but I wanted to show the power and easiness of tikz:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}

\foreach \x in {0,1,2,3}
\draw (\x,0)--(\x,3)  (0,\x)--(3,\x);

\foreach \x/\y in {0.5/2.5,1.5/2.5,2.5/2.5,2.5/1.5}
\node at (\x,\y) {\Large{\#}};

\draw[-latex] (.5,1.5)--(1.5,1.5);

\end{tikzpicture}
\caption{A picture}
\end{figure}

\end{document}

enter image description here

  • Thx for your solution on my question. It might be usefull to someone else. – K-Jtan Nov 24 '16 at 19:45

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.