如何将 x 轴标签的基线与周围文本对齐

如何将 x 轴标签的基线与周围文本对齐

此渲染:

在此处输入图片描述

制作方:

\documentclass{article}
\usepackage{showframe}
\usepackage{pgfplots}

\begin{document}

\begin{figure}[htb]
\begin{tikzpicture}[trim axis right, trim axis left]
\begin{axis}[
    xmin=-3,   xmax=3,
    ymin=-3,   ymax=3,
    extra x ticks={-1,1},
    extra y ticks={-2,2},
    extra tick style={grid=major},
    xlabel=This is the first text which is this long,
    height=3cm,
    width=\textwidth/2, scale only axis
]
\end{axis}
\end{tikzpicture}This is the second text
\end{figure}

\end{document}

我如何修剪底部,tikzpicture以便 x 轴标签的基线与周围文本的基线对齐?我知道这个[baseline=...]参数,tikzpicture但无法弄清楚如何从 pgfplot 中提取位置


注意:这里的实际动机是在轴标签和图形标题之间获得更小的空间,这里的答案成功了!

答案1

您可以相应地设置基线tikzpicture

\documentclass{article}
\usepackage{showframe}
\usepackage{pgfplots}

\begin{document}

\begin{figure}[htb]
\begin{tikzpicture}[trim axis right, trim axis left,baseline={(xlabel.base)}]
\begin{axis}[
    xmin=-3,   xmax=3,
    ymin=-3,   ymax=3,
    extra x ticks={-1,1},
    extra y ticks={-2,2},
    extra tick style={grid=major},
    xlabel=This is the first text which is this long,
    xlabel style={alias=xlabel},
    height=3cm,
    width=\textwidth/2, scale only axis
]
\end{axis}
\end{tikzpicture}This is the second text
\end{figure}

\end{document}

在此处输入图片描述

对于自动生成的图表你可以这样做:

\documentclass{article}
\usepackage{showframe}
\usepackage{pgfplots}
\newcounter{myplot}
\tikzset{set baseline/.code={\stepcounter{myplot}
\tikzset{baseline={(xlabel-\number\value{myplot}.base)},
/pgfplots/xlabel style={alias=xlabel-\number\value{myplot}}}
}}
\tikzset{every picture/.append style={set baseline}}
\begin{document}

\begin{figure}[htb]
\begin{tikzpicture}[trim axis right, trim axis left]
\begin{axis}[
    xmin=-3,   xmax=3,
    ymin=-3,   ymax=3,
    extra x ticks={-1,1},
    extra y ticks={-2,2},
    extra tick style={grid=major},
    xlabel=This is the first text which is this long,
    height=3cm,
    width=\textwidth/2, scale only axis
]
\end{axis}
\end{tikzpicture}This is the second text
\end{figure}

\end{document}

相关内容