4

How can I get a side caption for a Tikz drawing?

If I try with SCfigure my drawing overlaps with the caption.

Here is the immage I drew using SCfigure:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{sidecap}
\begin{document}
\begin{SCfigure} [b]
\centering
\begin{tikzpicture}[scale=0.5]
\draw  (-1,-3) -- (-1,3) -- (-0.7,3) -- (-0.7, -3) -- cycle;
\draw  (1,3) -- (1,-3) -- (0.7,-3) -- (0.7, 3) -- cycle;
 \fill[black!15] (1,3) -- (1,-3) -- (0.7,-3) -- (0.7, 3) -- cycle;
 \fill[black!15] (-1,-3) -- (-1,3) -- (-0.7,3) -- (-0.7, -3) -- cycle; 
\draw [<->] (-0.68,0) --(0.68,0);
\draw (0,0)  node[anchor=north]  {$h$};
\draw [<-] (1.01,-0.5) --(2.8,-0.5);
\draw (1.9,-0.5)  node[anchor=north]  {\small{K}};
\draw (1.75,0.5) circle (0.7);
\draw (2.2,2.5) circle (0.7);
\draw (-2,1.7) circle (0.7);
\draw (-1.75,0) circle (0.7);
\draw (-2.3,-1.6) circle (0.7);
\draw (2,-1.98) circle (0.7);
\draw [<->] (2.69,-1.98) --(1.31,-1.98);
\draw (2,-1.98)  node[anchor=north]  {$\sigma$};
\end{tikzpicture} 
\caption{I would love to have this caption on the left of my drawing :}
\label{entropic1}
\end{SCfigure}
\end{document}

enter image description here

  • 4
    (i) show small but complete document which demonstrate your problem (only with seeing it we can help you), (ii) it seems that your image is to wide ... (iii) welcome to tex.se – Zarko Feb 19 '18 at 14:13
  • Thank you! How can I make my image smaller ? – Andrea Cerfogli Feb 20 '18 at 9:48
  • 1
    @AndreaCerfogli change [scale=0.5] to the appropiate factor, and avoid the use of absolute units such as the cm you used in the circles. – JLDiaz Feb 20 '18 at 9:59
  • Can you show us how you tried to use SCfigure, there is no overlap when I try it here. Your diagram is certainly not so wide that you don't have room for the caption next to it, so resizing shouldn't be necessary. – Torbjørn T. Feb 20 '18 at 10:30
  • Latex gives me this error, but I don't know how to improve mi code Overfull \hbox (84.33546pt too wide) in paragraph at lines 35--35 – Andrea Cerfogli Feb 20 '18 at 11:24
2
  • as mentioned in above comments, for captions on side of figure (or table) is intended SCfigure
  • vertical position of caption reggarding image you can set with \sidecaptionvpos{figure}{m} (m for middle, t for top and b for bottom /default/)
  • with option you can determine ration between image and caption width (default is 1)

enter image description here

(red lines only indicate page layout)

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[leftcaption]{sidecap}
\sidecaptionvpos{figure}{m}
\usepackage{tikz}

%-------------------------------- show page layout, only for test
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{SCfigure}[0.8][htb]
\centering
    \begin{tikzpicture}%[baseline]
\draw[fill=black!15]  (-1,-3) -- (-1,3) -- (-0.7,3) -- (-0.7, -3) -- cycle;
\draw[fill=black!15]  (1,3) -- (1,-3) -- (0.7,-3) -- (0.7, 3) -- cycle;

\draw [<->] (-0.68,0) -- node[anchor=north]  {$h$}(0.68,0);
\draw [<-] (1.01,-0.5) -- node[anchor=north]  {$K$}(2.8,-0.5);

\draw   (1.75,0.5)  circle (0.7cm)
        (2.2,2.5)   circle (0.7cm)
        (2,-1.8)    circle (0.7cm);
        (-2,1.7)    circle (0.7cm);
        (-1.75,0)   circle (0.7cm);
        (-2.3,-1.6) circle (0.7cm);
\draw [<->] (2.69,-1.8) -- node[anchor=north]  {$\sigma$}(1.31,-1.8);
    \end{tikzpicture}
\caption{I would love to have this caption on the left of my drawing :)}
\label{plates1}
    \end{SCfigure}
\end{document}

in above code i also make tikzpicture code shorter. as you see, image scaling is not needed.

| improve this answer | |
1

You have a syntax error. The first option after \begin{SCfigure} should be a number (the relative size of the caption). You have [b].

This is the correct code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage[leftcaption]{sidecap}

\begin{document} 
\begin{SCfigure}[0.5][b]
\centering 
\begin{tikzpicture}[scale=1] 
    \draw  (-1,-3) -- (-1,3) -- (-0.7,3) -- (-0.7, -3) -- cycle;
    \draw  (1,3) -- (1,-3) -- (0.7,-3) -- (0.7, 3) -- cycle;
     \fill[black!15] (1,3) -- (1,-3) -- (0.7,-3) -- (0.7, 3) -- cycle;
     \fill[black!15] (-1,-3) -- (-1,3) -- (-0.7,3) -- (-0.7, -3) -- cycle; 
    \draw [<->] (-0.68,0) --(0.68,0);
    \draw (0,0)  node[anchor=north]  {$h$};
    \draw [<-] (1.01,-0.5) --(2.8,-0.5);
    \draw (1.9,-0.5)  node[anchor=north]  {\small{K}};
    \draw (1.75,0.5) circle (0.7);
    \draw (2.2,2.5) circle (0.7);
    \draw (-2,1.7) circle (0.7);
    \draw (-1.75,0) circle (0.7);
    \draw (-2.3,-1.6) circle (0.7);
    \draw (2,-1.98) circle (0.7);
    \draw [<->] (2.69,-1.98) --(1.31,-1.98);
    \draw (2,-1.98)  node[anchor=north]  {$\sigma$};
\end{tikzpicture}
\caption{I would love to have this caption on the left of my drawing} \label{entropic1} 
\end{SCfigure} 
\end{document}

Which produces:

Result

| improve this answer | |

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.