配置をそのまま維持しながら環境を介して図を配置するにはどうすればよいですか?

配置をそのまま維持しながら環境を介して図を配置するにはどうすればよいですか?

図の作成と参照を簡素化するために、次のコードを使用しています。

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

すべての図をcfig環境に置き換えると、ほとんどの図が別のページに移動されました遠いソースコード内の元の位置から離れています。なぜ図の配置は(明らかに)周囲の環境によって影響を受けるのでしょうか? の柔軟性をfigure完全に失うことなく、この動作を修正するにはどうすればよいでしょうか?

答え1

オプション リストに p がない場合、1 つの大きな数字によって、後続のすべての数字が最後に強制的に配置される可能性があります。例:

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

答え2

ドキュメント全体のすべての図を環境に囲む予定の場合はcfig、-環境を再定義するだけで済みます。また、とを環境内にfigure含める必要はなく、グローバルに定義できます (の代わりにを使用することもできます)。これらの変更が配置の問題に影響を与えるかどうかはわかりません。\crefsub\Crefsubfig.~\thefigure\cref

提案された変更は次のように行います。

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

関連情報