如何透過環境放置人物,同時保持放置完整?

如何透過環境放置人物,同時保持放置完整?

我使用以下程式碼來簡化圖形創建和引用。

\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,那麼一個大數字可以將所有後續數字強製到最後。舉個例子:

\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環境中,您也可以重新定義 -environment figure。此外,您不必將它們封閉\crefsub\Crefsub環境內部,它們可以全域定義(並且可能只使用fig.~\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}

相關內容