[Tex/LaTex] Position of figure and caption within itemize environment

positioning

How can I insert a figure within an itemize environment so that it's centered within the item, not the entire text width? I tried it with the figure environment first, but that gave me the figure too far on the left. Then I tried it with the center environment, but then I get the same problem with the caption (instead of the figure).

\documentclass[12pt]{article}
\usepackage{caption}
\usepackage{tikz}
\usepackage{blindtext}

\begin{document}
\begin{itemize}
    \item \blindtext
    \begin{center}
        \captionsetup{type=figure}
        \begin{tikzpicture}
        \clip(-5.686675053232457,-3.16893917565149) rectangle (5.579414724686105,0.6921754200738994);
        \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105]plot(\x,{(--4.514220105043796-1.8219356725415592*\x)/1.894960446786564});
        \draw [line width=1.2pt,dash pattern=on 4pt off 4pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--1.6188066144426525-1.0979714604086213*\x)/-0.3751399196759637});
        \draw [line width=1.2pt,dash pattern=on 4pt off 4pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--11.463481791986391--1.2426776436313691*\x)/-5.670646727250561});
        \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--0.4039090361160175--0.6586822831185679*\x)/-0.27723191082856635});
        \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(-2.9955647165449215--0.8716146065783191*\x)/4.89799733746727});    
        \end{tikzpicture}
        \caption{blablablablablablablablablablablablablablablablablablabla}
    \end{center}
\end{itemize}
\end{document}

Update: I think this could be a solution:

\begin{itemize}
    \item \blindtext
    \bigbreak
    \begin{minipage}{\linewidth}
        \centering
        \begin{tikzpicture}
        \clip(-5.686675053232457,-3.16893917565149) rectangle (5.579414724686105,0.6921754200738994);
        \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105]plot(\x,{(--4.514220105043796-1.8219356725415592*\x)/1.894960446786564});
        \draw [line width=1.2pt,dash pattern=on 4pt off 4pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--1.6188066144426525-1.0979714604086213*\x)/-0.3751399196759637});
        \draw [line width=1.2pt,dash pattern=on 4pt off 4pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--11.463481791986391--1.2426776436313691*\x)/-5.670646727250561});
        \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--0.4039090361160175--0.6586822831185679*\x)/-0.27723191082856635});
        \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(-2.9955647165449215--0.8716146065783191*\x)/4.89799733746727});    
        \end{tikzpicture}
        \captionsetup{type=figure}
        \caption{blablablablablablablablablablablablablablablabla}
    \end{minipage}
    \bigbreak
\end{itemize}

Best Answer

An adjustbox provides a lot of functionality within a list environment such as itemize.

Passing the following keys to adjustbox gives you what you described:

  1. A figure centered within the width of the line: center=\linewidth
  2. Spacing before and after (bigskip replacement): margin=0ex 2ex 0ex 2ex
  3. Identify the tikzpicture as a figure, but don't make it float: nofloat=figure
  4. Provide a figure caption: caption=This figure does not float. It is placed between the two items
  5. Add a decorative box: cfbox=blue 1pt

In response to questions from the OP, a second example is provided.

  1. The tikzpicture is treated as floating figure: figure=htb, where htb are the placement options for the figure.
  2. The caption is labelled: \label{fig:MyFigure}.
  3. The caption is moved to the top of the figure: captionabove={This figure floats. Its placement is determined by page content and thefigure=htbsettings}.
  4. Added the hyperref package to create a link between a position in the text (\ref{fig:MyFigure}) and the figure caption.

I encountered an issue where the caption in the second example was no longer constrained within the linewidth of the itemed entry. This was the result of changing nofloat=figure to figure=htp. I was only able to address this by a hack, which was:

  1. Loaded enumitem and caption packages
  2. Adding \captionsetup{margin=0.5cm}
  3. Adding [leftmargin=*]

I am sure this can be improved because I just experimented until something worked.

Following are the results for each example. Note that the second example still places the figure between the two items because h was used as a placement option, and because it fits in relation to the page content.

Try changing \Blindtext[1][2] to \Blindtext[3][2]. There is not enough space on the page to place the figure so it floats to the next page, while the text remains on the first page.

This is the output for each example:

enter image description here

This is the code:

\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usepackage[english]{babel}         % for pangram from blindtext package
\usepackage[pangram]{blindtext}
\usepackage{adjustbox}
\usepackage{caption}
\usepackage{enumitem}
\usepackage{hyperref}

\begin{document}

\begin{itemize}%[leftmargin=*] % Dont use leftmargin setting. It upsets the caption alignment
    \item[***] \textbf{Example 1}
    \item \Blindtext[1][2]

        \begin{adjustbox}{
                cfbox=blue 1pt,     % draw a box around the picture
                center=\linewidth,  % center the picture in the with of the line
                margin=0ex 2ex 0ex 2ex, % add some space above and below the figure
                caption=This figure does not float. It is placed between the two items.,  % give the figure caption
                nofloat=figure     % this is a non-floating figure          
            }
            \begin{tikzpicture}
            \clip(-5.686675053232457,-3.16893917565149) rectangle (5.579414724686105,0.6921754200738994);
            \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105]plot(\x,{(--4.514220105043796-1.8219356725415592*\x)/1.894960446786564});
            \draw [line width=1.2pt,dash pattern=on 4pt off 4pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--1.6188066144426525-1.0979714604086213*\x)/-0.3751399196759637});
            \draw [line width=1.2pt,dash pattern=on 4pt off 4pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--11.463481791986391--1.2426776436313691*\x)/-5.670646727250561});
            \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--0.4039090361160175--0.6586822831185679*\x)/-0.27723191082856635});
            \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(-2.9955647165449215--0.8716146065783191*\x)/4.89799733746727});    
            \end{tikzpicture}
        \end{adjustbox}
%
    \item \Blindtext[1][2]
\end{itemize}

% A new example added with the figure as a float, a label for the figire and a reference to the figure

\begin{itemize}[leftmargin=*] %Add left margin setting to get correct caption alignment
    \item[***] \textbf{Example 2}
    \item \Blindtext[1][2]
    \item This is a reference to Figure \ref{fig:MyFigure}.
    \captionsetup{margin=0.5cm}
    \begin{adjustbox}{
            cfbox=green 1pt,     % draw a box around the picture
            center=\linewidth,  % center the picture in the with of the line
            margin=0ex 2ex 0ex 2ex, % add some space above and below the figure
            captionabove={This figure floats. Its placement is determined by page content and the figure=htb settings.} \label{fig:MyFigure},  % give the figure caption and a label to link to
            figure=htb      % this is a floating figure with placement options                     
        }
        \begin{tikzpicture}
        \clip(-5.686675053232457,-3.16893917565149) rectangle (5.579414724686105,0.6921754200738994);
        \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105]plot(\x,{(--4.514220105043796-1.8219356725415592*\x)/1.894960446786564});
        \draw [line width=1.2pt,dash pattern=on 4pt off 4pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--1.6188066144426525-1.0979714604086213*\x)/-0.3751399196759637});
        \draw [line width=1.2pt,dash pattern=on 4pt off 4pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--11.463481791986391--1.2426776436313691*\x)/-5.670646727250561});
        \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(--0.4039090361160175--0.6586822831185679*\x)/-0.27723191082856635});
        \draw [line width=1.2pt,domain=-5.686675053232457:9.579414724686105] plot(\x,{(-2.9955647165449215--0.8716146065783191*\x)/4.89799733746727});    
        \end{tikzpicture}
    \end{adjustbox}
    %
    \item \Blindtext[1][2]
\end{itemize}

\end{document}