1

This rendering:

enter image description here

is produced by:

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

How can I trim the bottom of the tikzpicture so that the baseline of the x-axis label is aligned with the baseline of the surrounding text? I'm aware of the [baseline=...] argument to tikzpicture but can't work out how to extract a position from the pgfplot


Note: the actual motivation here is to get a smaller space between the axis label and the figure caption, which the answers here succeed in!

1 Answer 1

2

You can set the baseline of the tikzpicture accordingly.

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

enter image description here

For automatically generated plots you could do something like this:

\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}
5
  • Wonderful, thanks! Will this work for plots using groupplots too, or will I need something like r1c1.xlabel.base?
    – Eric
    Jan 14, 2020 at 0:21
  • Ah, now I see, you've used alias - is there a way to do this without adding the alias? My tikzpicture is generated, and I'd prefer not to have to insert that. Perhaps a hierarchical name of some kind?
    – Eric
    Jan 14, 2020 at 0:23
  • @Eric I used xlabel style={alias=xlabel}, to give the node a name. As long as the name you give is unique, it should work. You can use some \pgfplotsset to set these things from outside if you have automatically generated plots. I do not know how you generate them, can you be more specific? In any case, you need to make them unique.
    – user194703
    Jan 14, 2020 at 0:23
  • Yep, just realized that - see my above comment.
    – Eric
    Jan 14, 2020 at 0:24
  • 1
    @Eric I added a solution that works for automatically generated plots. You do not even have to touch the ambient tikzpictures explicitly.
    – user194703
    Jan 14, 2020 at 0:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.