1

I want to adjust the position of rectangle in the caption of a tikz figure. It should be go down slightly. Also in caption, it is coming "Fig.X 1-". I want to remove
extra "1 -". How to do these? This is my code

\documentclass[preview]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

%\usepackage{caption}
\usepackage[labelsep=endash]{caption}
\renewcommand{\figurename}{Fig X.}

\begin{document}

\begin{figure}
\begin{tikzpicture}[scale=0.18]
\foreach \x in {1,...,50}
\fill[red] (1.1*\x,3) rectangle (1.1*\x+1,0);
\foreach \x in {1,5,7,10,16,22}
\fill[blue] (1.1*\x,3) rectangle (1.1*\x+1,0);
\foreach \x in {2,14,35,44,46}
\fill[green] (1.1*\x,3) rectangle (1.1*\x+1,0);
\end{tikzpicture}
\caption{ \begin{tikzpicture}[scale=0.18] \fill[gray!=10] (0, 3 ) rectangle (0.75, 0 ); \end{tikzpicture}: X }

\end{figure}
\end{document}
1
  • I assume you are using standalone while debugging, since the figure counter in only meaningful in the main document. Feb 9, 2022 at 15:16

1 Answer 1

3

Like this?

enter image description here

Before tikzpicture in caption you should insert command \protect:

\documentclass[preview]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\usepackage[labelsep=endash]{caption}
\renewcommand{\figurename}{Fig X.}

\begin{document}
    \begin{figure}
    \centering
\begin{tikzpicture}[scale=0.18]
\foreach \x in {1,...,50}
\fill[red] (1.1*\x,3) rectangle (1.1*\x+1,0);
\foreach \x in {1,5,7,10,16,22}
\fill[blue] (1.1*\x,3) rectangle (1.1*\x+1,0);
\foreach \x in {2,14,35,44,46}
\fill[green] (1.1*\x,3) rectangle (1.1*\x+1,0);
\end{tikzpicture}
\caption{\protect\tikz{\fill[gray!30] (0,0) rectangle (0.15,2ex);}: X }
    \end{figure}
\end{document}

Off-topic: I would draw your image without scaling, with white borders of rectangles and with relative coordinates for their sizes:

\documentclass[preview]{standalone}
\usepackage{tikz}
\usepackage[labelsep=endash]{caption}
\renewcommand{\figurename}{Fig X.}

\begin{document}
    \begin{figure}
    \centering
\begin{tikzpicture}
\foreach \x in {1,...,50}
    \draw[white,fill=red] (0.2*\x,0)   rectangle ++(0.2,0.6);
\foreach \x in {1,5,7,10,16,22}
    \draw[white,fill=blue] (0.2*\x,0)  rectangle ++(0.2,0.6);
\foreach \x in {2,14,35,44,46}
    \draw[white,fill=green] (0.2*\x,0) rectangle ++(0.2,0.6);
\end{tikzpicture}
\caption{\protect\tikz{\fill[gray!30] (0,0) rectangle (0.15,2ex);}: X }
    \end{figure}
\end{document}

The compilation result is similar as before:

enter image description here

4
  • Thank you so much. How to remove "1 -" from the caption?
    – user12290
    Feb 9, 2022 at 9:35
  • 2
    That means, that X is actually figure number? This problem is not mentioned in your question ... and probably deserve new question. Try \caption*{Fig X. -- \protect\tikz{\fill[gray!30] (0,0) rectangle (0.15,2ex);}: X }
    – Zarko
    Feb 9, 2022 at 9:45
  • Thanks a lot. Yes it is working.
    – user12290
    Feb 9, 2022 at 9:56
  • 1
    Or \renewcommand\thefigure{X} or possibly \renewcommand{\thefigure}{\Roman{figure}} for figure 10. Feb 9, 2022 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .