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 asitemize
.Passing the following keys to
adjustbox
gives you what you described:center=\linewidth
bigskip
replacement):margin=0ex 2ex 0ex 2ex
tikzpicture
as a figure, but don't make it float:nofloat=figure
caption=This figure does not float. It is placed between the two items
cfbox=blue 1pt
In response to questions from the OP, a second example is provided.
tikzpicture
is treated as floating figure:figure=htb
, where htb are the placement options for the figure.\label{fig:MyFigure}
.captionabove={This figure floats. Its placement is determined by page content and the
figure=htbsettings}
.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
tofigure=htp
. I was only able to address this by a hack, which was:enumitem
andcaption
packages\captionsetup{margin=0.5cm}
[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:
This is the code: