
tikz
일부 매개변수를 사용하여 텍스트가 있는 페이지에 대해 가능한 최대 크기로 그림의 크기를 조정할 수 있습니까 ? 예를 들어, 다음과 같은 몇 가지 매개변수가 있습니까?
\usepackage{float}
\usepackage{tikz, pgfplots}
\begin{document}
\begin{figure}[H]
\begin{tikzpicture}[scale = **SOMETHING_HERE_TO_AUTO_SCALE_TO_MAX_SIZE_ON_PAGE?**]
% ... code to draw figure
\end{tikzpicture}
\end{figure}
\end{document}
내가 이것을 달성한 유일한 방법은 시행착오를 통해서였습니다.
이 말은 내가 노력한다는 뜻이다
\begin{figure}[H]
\begin{tikzpicture}[scale = 1]
% ... code to draw figure
\end{tikzpicture}
\end{figure}
페이지에 딱 맞는데 더 크게 만들 수 있나요? 노력하다
\begin{figure}[H]
\begin{tikzpicture}[scale = 2]
% ... code to draw figure
\end{tikzpicture}
\end{figure}
너무 커서 크기를 줄였어요
\begin{figure}[H]
\begin{tikzpicture}[scale = 1.5]
% ... code to draw figure
\end{tikzpicture}
\end{figure}
좋아 또 맞네 더 크게 만들 수 있나요?
... 등.
라텍스가 이미 존재하는 모든 텍스트, 기타 그림 등을 포함하여 현재 페이지에서 가능한 최대 크기로 그림을 자동 조정하는 쉬운 방법이 있습니까?
답변1
삽입된 그림은 어느 정도 "고정"되어 있습니다. 그래서 나는 figure
환경이나 다른 환경을 사용하지 않습니다 float
.
아이디어는 간단합니다. 페이지의 남은 높이를 계산합니다.
\dimexpr\pagegoal-\pagetotal-#1\relax
center
#1은 환경 을 위해 예약된 공간 caption
이며 4\baselineskip
기본적으로 입니다.
그런 다음 너비 와 높이 tikzpicture
의 상자에 맞춥니다 .\textwidth
\dimexpr\pagegoal-\pagetotal-#1\relax
\getpicdimen
사진의 원래 크기를 가져오는 데 사용됩니다.\fittobox
고정된 너비와 높이의 상자에 그림을 맞추는 데 사용됩니다.\fitremaining
페이지의 나머지 공간에 그림을 맞추는 데 사용됩니다.
\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\usepackage{showframe}
\usepackage{lipsum}
\usetikzlibrary{fit, calc, positioning}
\usepackage{xparse}
\renewcommand*\ShowFrameColor{\color{red}}
\NewDocumentCommand { \getpicdimen } { s O{\picwidth} O{\picheight} +m }
{
\begin{pgfinterruptboundingbox}
\begin{scope}[local bounding box=pic, opacity=0]
\IfBooleanTF {#1}
{ \node[inner sep=0pt, fit=(#4)] {}; }
{ #4 }
\end{scope}
\path ($(pic.north east)-(pic.south west)$);
\end{pgfinterruptboundingbox}
\pgfgetlastxy{#2}{#3}
}
\ExplSyntaxOn
\fp_new:N \l__scale_fp
\NewDocumentCommand { \fittobox } { O{\picwidth} O{\picheight} m m D(){0, 0} +m }
{
\getpicdimen[#1][#2]{#6}
\fp_compare:nTF
{
% pic ratio
\dim_ratio:nn { #1 } { #2 } >
% box ratio
\dim_ratio:nn { #3 } { #4 }
}
{ \fp_set:Nn \l__scale_fp { \dim_ratio:nn { #3 } { #1 } } }
{ \fp_set:Nn \l__scale_fp { \dim_ratio:nn { #4 } { #2 } } }
\begin{scope}[
shift={($(#5) - \fp_use:N \l__scale_fp*(pic.center)$)},
scale=\fp_use:N \l__scale_fp,
nodes={transform~shape},
]
#6
\end{scope}
}
\NewDocumentCommand { \fitremaining } { O{ 4\baselineskip } +m }
{
\fittobox{\textwidth}{\dimexpr\pagegoal-\pagetotal-#1\relax}{#2}
}
\ExplSyntaxOff
\begin{document}
\lipsum[1]
\begin{center}
\begin{tikzpicture}
\fitremaining{
\draw (0, 0) node[left, draw] {$A$} -- (1, 1) node[right, draw] {$B$};
}
\end{tikzpicture}
\captionof{figure}{Test figure}
\end{center}
Some other text.
\clearpage
\lipsum[1-3]
\begin{center}
\begin{tikzpicture}
\fitremaining{
\draw (0, 0) node[left, draw] {$A$} -- (1, 1) node[right, draw] {$B$};
}
\end{tikzpicture}
\captionof{figure}{Test figure}
\end{center}
\clearpage
\end{document}