Como posicionar a figura através do ambiente mantendo o posicionamento intacto?

Como posicionar a figura através do ambiente mantendo o posicionamento intacto?

Estou usando o código a seguir para simplificar a criação e referência 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}%
}

Ao substituir todas as minhas figuras pelo cfigambiente, a maioria das figuras foram movidas para páginas separadasdistantelonge de sua posição original no código-fonte. Por que o posicionamento das figuras é (aparentemente) afetado pelo ambiente circundante? Como posso corrigir esse comportamento sem perder figurecompletamente a flexibilidade?

Responder1

Se você não tiver p na lista de opções, um número grande poderá forçar todos os números seguintes até o final. Como um exemplo:

\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}

Responder2

Se você planeja incluir todas as figuras de todo o documento em seu cfigambiente, você também pode redefinir o figure-environment. Além disso, você não precisa incluir \crefsube \Crefsubdentro do ambiente, eles podem ser definidos globalmente (e podem ser usados ​​apenas fig.~\thefigureem vez de \cref). Não sei se essas alterações têm impacto no seu problema de posicionamento.

Eu faria as alterações propostas assim:

\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}

informação relacionada