¿Cómo colocar la figura a través del entorno manteniendo intacta la ubicación?

¿Cómo colocar la figura a través del entorno manteniendo intacta la ubicación?

Estoy usando el siguiente código para simplificar la creación y referencia de figuras.

\newenvironment{figcontext}[1]{%
\newcommand{\thefig}{#1}%
\newcommand{\crefsub}[1]{\cref{\thefig}.##1}%
\newcommand{\Crefsub}[1]{\Cref{\thefig}.##1}%
}{}

\newenvironment{cfig}[1]{%
\begin{figcontext}{#1}%
\begin{figure}[h!tb]% only 'H' is respected
\centering%
}{%
\label{\thefig}%
\end{figure}%
\end{figcontext}%
}

Al reemplazar todas mis figuras con el cfigentorno, la mayoría de las figuras se movieron a páginas separadas.lejoslejos de su posición original en el código fuente. ¿Por qué la ubicación de las figuras (aparentemente) se ve afectada por el entorno? ¿Cómo puedo solucionar este comportamiento sin perder la flexibilidad por figurecompleto?

Respuesta1

Si no tiene p en la lista de opciones, entonces una figura grande puede forzar todas las figuras siguientes hasta el final. Como ejemplo:

\documentclass{article}
\usepackage{lipsum}

\begin{document}
\lipsum[1]

\begin{figure}[thb!] %compare with [thb!p]
\rule{1cm}{0.98\textheight}
\caption{blub}
\end{figure}

\lipsum[1]
\begin{figure}[thb!]
blbl
\caption{blub}
\end{figure}

\lipsum\lipsum
\end{document}

Respuesta2

Si planea incluir cada figura de todo su documento en su cfigentorno, también podría simplemente redefinir el figureentorno. Además, no es necesario encerrarlos \crefsubdentro \Crefsubdel entorno, se pueden definir globalmente (y podrían usarse fig.~\thefigureen lugar de \cref). No sé si estos cambios tienen un impacto en su problema de ubicación.

Haría los cambios propuestos de esta manera:

\documentclass{article}

% for MWE
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{todonotes}
\usepackage{tikz}

\usepackage{cleveref}

%\newcommand{\crefsub}[1]{fig.~\thefigure.#1}% without using cref
%\newcommand{\Crefsub}[1]{Figure~\thefigure.#1}% without using cref
\newcommand{\crefsub}[2][\myfiglabel]{\cref{#1}.#2}% with using cref
\newcommand{\Crefsub}[2][\myfiglabel]{\Cref{#1}.#2}% with using cref

\let\figurebak\figure
\let\endfigurebak\endfigure
\renewenvironment{figure}[2][tbhp]{
    \def\myfiglabel{#2}%
    \begin{figurebak}[#1]%
        \centering%
}{%
        \label{\myfiglabel}%
    \end{figurebak}%
}


\begin{document}

\blindtext[1]

\blindtext[1]

\blindtext[1]

\begin{figure}{fig:label}
    \begin{tikzpicture}[yscale=1.5]
        \draw [help lines, <->]  (0,0) -- (6.5,0) node{a};
        \draw [help lines, ->] (0,-1.1) -- (0,1.1) node{b};
        \draw [green,domain=0:2*pi] plot (\x, {(sin(\x r)* ln(\x+1))/2});
        \draw [red,domain=0:pi] plot (\x, {sin(\x r)});
        \draw [blue, domain=pi:2*pi] plot (\x, {cos(\x r)*exp(\x/exp(2*pi))});
    \end{tikzpicture}
    \caption{caption to my fig. Please note \Crefsub{a} and \crefsub{b}}
\end{figure}

See \cref{fig:label} as well as \crefsub[fig:label]{a}.

\blindtext[1]

\blindtext[1]

\blindtext[1]


\end{document}

información relacionada