我正在尝试为同事准备一个图表。我已经创建了图表,但想裁剪掉周围的空白。
我曾尝试使用standalone
文档类,但这会导致tikzpicture
s 并排出现,而不是彼此叠加。
有没有更好的方法来裁剪这个 PDF?
例子:
\documentclass[border=2cm]{standalone}
\usepackage[scaled]{helvet}
\renewcommand\familydefault{\sfdefault}
\usepackage[T1]{fontenc}
\usepackage[svgnames,dvipsnames,x11names]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usepackage{geometry}
\pgfplotsset{ every non boxed x axis/.append style={x axis line style=-} }
\pgfplotsset{ every non boxed y axis/.append style={y axis line style=-} }
\usepackage[eulergreek]{sansmath}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.12}
\pgfplotsset
{
tick label style = {font=\sansmath\sffamily},
every axis label = {font=\sansmath\sffamily},
tick style={black},
}
\renewcommand{\familydefault}{\sfdefault}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}[trim axis left]
\pgfplotsset{set layers}
\begin{axis}[
axis on top=true,
width=14cm,
height=8cm,
ylabel=\textbf{A},
title={Title},
xlabel=\textbf{B},
xmin = -0.2, xmax = 10,
xtick={0,1,...,10},
minor x tick num=4,
minor y tick num=4,
ymin = 0, ymax = 350,
ytick={0,25,...,350},
axis y line=left,
axis x line=bottom,
major tick style = {semithick, black},
legend style={draw=none, font=\small},
]
\end{axis}
\begin{axis}[
axis on top=true,
width=14cm,
height=8cm,
ylabel=\textbf{C},
xmin = -0.2, xmax = 10,
xtick={0,1,...,10},
minor x tick num=4,
minor y tick num=4,
ymin = 25, ymax = 135,
ytick={35,45,...,135},
axis y discontinuity=crunch,
major tick style = {semithick, black},
legend style={draw=none},
axis y line=right,
axis x line=none,
]
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}[trim axis left]
\begin{axis}
[
width=14cm,
domain=-0.2:10,
axis x line=bottom,
axis y line=none,
xlabel=\textbf{Fraction},
ymin = 0, ymax = 1,
tick label style={font=\tiny},
xtick={-0.002267,0.498043,0.997959,1.102242,1.310256,2.060156,2.810043,3.561611,4.311513,5.061414,5.811329,6.561231,7.312812,7.959396,10},
xticklabel=\empty,
major tick style = {semithick, black},
axis line style=black,thick,
extra x ticks={0.247888,0.748001,1.0501005,1.206249,1.685206,2.4350995,3.185827,3.936562,4.6864635,5.4363715,6.18628,6.9370215,7.636104,8.979698},
extra x tick labels={2B,3B,4B,5B,6B,7B,8B,9B,10B,11B,12B,13B,14B,Waste},
extra x tick style={major tick length=0pt}
]
\addplot [color=black]{0};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我怀疑你喜欢获得这样的东西:
\documentclass{article} % <---
\usepackage[scaled]{helvet}
\renewcommand\familydefault{\sfdefault}
\usepackage[T1]{fontenc}
\usepackage[svgnames,dvipsnames,x11names]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.16,
tick label style = {font=\sansmath\sffamily},
every axis label = {font=\sansmath\sffamily},
tick style = black,
every non boxed x axis/.append style={x axis line style=-},
every non boxed y axis/.append style={y axis line style=-},
}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns, positioning}
\usepackage{geometry}
\usepackage[eulergreek]{sansmath}
\usepackage[active,tightpage]{preview} % <---
\PreviewEnvironment{tikzpicture} % <---
\setlength\PreviewBorder{0pt} % <---
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}%[trim axis left]
\pgfplotsset{set layers}
\begin{axis}[
axis on top=true,
width=14cm,
height=8cm,
ylabel=\textbf{A},
title={Title},
xlabel=\textbf{B},
xmin = -0.2, xmax = 10,
xtick={0,1,...,10},
minor x tick num=4,
minor y tick num=4,
ymin = 0, ymax = 350,
ytick={0,25,...,350},
axis y line=left,
axis x line=bottom,
major tick style = {semithick, black},
legend style={draw=none, font=\small},
]
\end{axis}
\begin{axis}[
axis on top=true,
width=14cm,
height=8cm,
ylabel=\textbf{C},
xmin = -0.2, xmax = 10,
xtick={0,1,...,10},
minor x tick num=4,
minor y tick num=4,
ymin = 25, ymax = 135,
ytick={35,45,...,135},
axis y discontinuity=crunch,
major tick style = {semithick, black},
legend style={draw=none},
axis y line=right,
axis x line=none,
]
\end{axis}
\begin{axis}
[
axis x line shift=0.13, % <--- shift of x axis
width=14cm,
domain=-0.2:10,
axis x line=bottom,
axis y line=none,
xlabel=\textbf{Fraction},
ymin = 0, ymax = 1,
tick label style={font=\tiny},
xtick={-0.002267,0.498043,0.997959,1.102242,1.310256,2.060156,2.810043,3.561611,4.311513,5.061414,5.811329,6.561231,7.312812,7.959396,10},
xticklabel=\empty,
major tick style = {semithick, black},
axis line style=black,thick,
extra x ticks={0.247888,0.748001,1.0501005,1.206249,1.685206,2.4350995,3.185827,3.936562,4.6864635,5.4363715,6.18628,6.9370215,7.636104,8.979698},
extra x tick labels={2B,3B,4B,5B,6B,7B,8B,9B,10B,11B,12B,13B,14B,Waste},
extra x tick style={major tick length=0pt}
]
\addplot [color=black]{0};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
欢迎使用 TeX-SE!您可以直接在其中添加第二个轴tikzpicture
,然后将其移动到您想要的任何位置。
\documentclass[border=2cm]{standalone}
\usepackage[scaled]{helvet}
\renewcommand\familydefault{\sfdefault}
\usepackage[T1]{fontenc}
\usepackage[svgnames,dvipsnames,x11names]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
%\usepackage{geometry}
\pgfplotsset{ every non boxed x axis/.append style={x axis line style=-} }
\pgfplotsset{ every non boxed y axis/.append style={y axis line style=-} }
\usepackage[eulergreek]{sansmath}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.12}
\pgfplotsset{ tick label style = {font=\sansmath\sffamily},
every axis label = {font=\sansmath\sffamily},
tick style={black},
}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}
\begin{tikzpicture}[]
\pgfplotsset{set layers}
\begin{axis}[trim axis left,
axis on top=true,
width=14cm,
height=8cm,
ylabel=\textbf{A},
title={Title},
xlabel=\textbf{B},
xmin = -0.2, xmax = 10,
xtick={0,1,...,10},
minor x tick num=4,
minor y tick num=4,
ymin = 0, ymax = 350,
ytick={0,25,...,350},
axis y line=left,
axis x line=bottom,
major tick style = {semithick, black},
legend style={draw=none, font=\small},
]
\end{axis}
\begin{axis}[
axis on top=true,
width=14cm,
height=8cm,
ylabel=\textbf{C},
xmin = -0.2, xmax = 10,
xtick={0,1,...,10},
minor x tick num=4,
minor y tick num=4,
ymin = 25, ymax = 135,
ytick={35,45,...,135},
axis y discontinuity=crunch,
major tick style = {semithick, black},
legend style={draw=none},
axis y line=right,
axis x line=none,
]
\end{axis}
\begin{axis}[at={(0,-8cm)},trim axis left]
[
width=14cm,
domain=-0.2:10,
axis x line=bottom,
axis y line=none,
xlabel=\textbf{Fraction},
ymin = 0, ymax = 1,
tick label style={font=\tiny},
xtick={-0.002267,0.498043,0.997959,1.102242,1.310256,2.060156,2.810043,3.561611,4.311513,5.061414,5.811329,6.561231,7.312812,7.959396,10},
xticklabel=\empty,
major tick style = {semithick, black},
axis line style=black,thick,
extra x ticks={0.247888,0.748001,1.0501005,1.206249,1.685206,2.4350995,3.185827,3.936562,4.6864635,5.4363715,6.18628,6.9370215,7.636104,8.979698},
extra x tick labels={2B,3B,4B,5B,6B,7B,8B,9B,10B,11B,12B,13B,14B,Waste},
extra x tick style={major tick length=0pt}
]
\addplot [color=black]{0};
\end{axis}
\end{tikzpicture}
\end{document}
答案3
为了看到差异,您的示例输出使用背景颜色:
如果您的 TeX 编辑器支持magic comments
,您可以使用 裁剪您的 pdf,例如 ,pdfcrop.exe
它应该包含在您的Latex
发行版中。对于TeXstudio
、Windows
和pdflatex
,TeXlive
之后的相同示例输出pdfcrop.exe
:
% !TeX TXS-program:pdflatex = "C:\texlive2019\bin\win32\pdflatex.exe" -interaction=nonstopmode %.tex | "C:\texlive2019\bin\win32\pdfcrop.exe" %.pdf %.pdf
因此,您必须将此命令放在文件的开头:
% !TeX TXS-program:pdflatex = "C:\texlive2019\bin\win32\pdflatex.exe" -interaction=nonstopmode %.tex | "C:\texlive2019\bin\win32\pdfcrop.exe" %.pdf %.pdf
\documentclass[border=2cm]{standalone}
\usepackage[scaled]{helvet}
\renewcommand\familydefault{\sfdefault}
\usepackage[T1]{fontenc}
\usepackage[svgnames,dvipsnames,x11names]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usepackage{geometry}
\pgfplotsset{ every non boxed x axis/.append style={x axis line style=-} }
\pgfplotsset{ every non boxed y axis/.append style={y axis line style=-} }
\usepackage[eulergreek]{sansmath}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.12}
\pgfplotsset
{
tick label style = {font=\sansmath\sffamily},
every axis label = {font=\sansmath\sffamily},
tick style={black},
}
\renewcommand{\familydefault}{\sfdefault}
\pagestyle{empty}
\begin{document}
\colorbox{gray!20}{% added
\begin{tikzpicture}%[trim axis left]
\pgfplotsset{set layers}
\begin{axis}[
axis on top=true,
width=14cm,
height=8cm,
ylabel=\textbf{A},
title={Title},
xlabel=\textbf{B},
xmin = -0.2, xmax = 10,
xtick={0,1,...,10},
minor x tick num=4,
minor y tick num=4,
ymin = 0, ymax = 350,
ytick={0,25,...,350},
axis y line=left,
axis x line=bottom,
major tick style = {semithick, black},
legend style={draw=none, font=\small},
]
\end{axis}
\begin{axis}[
axis on top=true,
width=14cm,
height=8cm,
ylabel=\textbf{C},
xmin = -0.2, xmax = 10,
xtick={0,1,...,10},
minor x tick num=4,
minor y tick num=4,
ymin = 25, ymax = 135,
ytick={35,45,...,135},
axis y discontinuity=crunch,
major tick style = {semithick, black},
legend style={draw=none},
axis y line=right,
axis x line=none,
]
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}%[trim axis left]
\begin{axis}
[
width=14cm,
domain=-0.2:10,
axis x line=bottom,
axis y line=none,
xlabel=\textbf{Fraction},
ymin = 0, ymax = 1,
tick label style={font=\tiny},
xtick={-0.002267,0.498043,0.997959,1.102242,1.310256,2.060156,2.810043,3.561611,4.311513,5.061414,5.811329,6.561231,7.312812,7.959396,10},
xticklabel=\empty,
major tick style = {semithick, black},
axis line style=black,thick,
extra x ticks={0.247888,0.748001,1.0501005,1.206249,1.685206,2.4350995,3.185827,3.936562,4.6864635,5.4363715,6.18628,6.9370215,7.636104,8.979698},
extra x tick labels={2B,3B,4B,5B,6B,7B,8B,9B,10B,11B,12B,13B,14B,Waste},
extra x tick style={major tick length=0pt}
]
\addplot [color=black]{0};
\end{axis}
\end{tikzpicture}
} % from \colorbox
\end{document}
编译时会出现警告,提示编译命令将被覆盖。请回答 (a) allow for this document
:
请ok
按下一个警告继续编译(您不需要更改options
菜单中的命令配置)。
之后TeXstudio
自动将 document-id 放置magic comment
在文件顶部(例如):
% !TeX document-id = {b55a4920-174f-4c2e-be5a-b002ab325eb5}
裁剪的另一种可能性是使用命令行,甚至使用 windows batch file
,请参阅pdfcrop 的 Windows 批处理文件 - GitHub。
如果要将第二个放置x-axis
在第一个下方x-axis
,请参阅扎尔科的解决方案,如果你想将 tikzpictures 放在彼此之上,请参阅土拨鼠的解决方案。
额外的:您也可以使用latexmk.exe -c
它来清理辅助文件:
"C:\texlive2019\bin\win32\latexmk.exe" -c
那是:
% !TeX TXS-program:pdflatex = "C:\texlive2019\bin\win32\pdflatex.exe" -interaction=nonstopmode %.tex | "C:\texlive2019\bin\win32\pdfcrop.exe" %.pdf %.pdf | "C:\texlive2019\bin\win32\latexmk.exe" -c