2

I'm having issues finding how to vertically align a an inline graph created using pgfplots. Right now the bottom of the graph is aligned with the baseline of the text. I'd rather have it so that the top of the graph is aligned with the top of the text.

I'm brand new to this, and I am not not even sure what I am looking for to figure out how to change it. I've tried searching for every permutation of "inline vertical alignment pgfplot" that I can think of, and all I can find relates to aligning one graph to another, rather than to the surrounding text.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{cartesian/.append style={axis x line=middle, axis y line=middle, xlabel={$x$}, ylabel={$y$}, axis equal}}
\begin{document}
\begin{enumerate}
    \setcounter{enumi}{16}
    \item
        \begin{tikzpicture}
            \begin{axis}[cartesian, xmin=-3, xmax=3, ymin=-3, ymax=3, xtick={-2,-1,...,2}, ytick={-2,-1,...,2}]
                \addplot [black, mark=*] coordinates {(0,0) (2,1)};
            \end{axis}
        \end{tikzpicture}
\end{enumerate}
\end{document}

It outputs

cartesian plot vertically aligned with the baseline of surrounding text

If anyone can provide me with some direction on how to fix my alignment issue, that would be amazing. I feel like this is really basic, but I don't know enough to even know what to search for.

1

As caverac already mentioned in his answer key to your goal is the baseline option which has to be added to the tikzpicture environment. Then it is a matter of taste how to do the rest.

For details please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt,varwidth]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        cartesian/.append style={
            axis lines=middle,
            xlabel={$x$},
            ylabel={$y$},
            axis equal,
        },
    }
\begin{document}
\begin{enumerate}
    \setcounter{enumi}{16}
    \item \dots
        \begin{tikzpicture}[
            % add `baseline' option to shift `tikzpicture' to the baseline of
            % the embedding text ...
            baseline,
        ]
            \begin{axis}[
                % ... then you can change the anchor of the `axis' environment
                % from the default `south west' to `north west' which aligns
                % the top of the plot with the baseline of the embedding text. ...
                anchor=north west,
                % ... Then it is left to shift the `tikzpciture'/`axis'
                % environment to the top of the text.
                yshift=1.5ex,
                %
                cartesian,
                % (don't use these ...
%                xtick={-2,-1,...,2},
%                ytick={-2,-1,...,2},
                % ... but that ...
                xtick distance=1,
                ytick distance=1,
                % and change the limits of the axis a bit to avoid the
                % 3's to be printed)
                xmin=-2.99,
                xmax=2.99,
                ymin=-2.99,
                ymax=2.99,
            ]
                \addplot [black, mark=*] coordinates {(0,0) (2,1)};

                % for debugging purposes only
                \draw [help lines] (rel axis cs:0,1) -- (rel axis cs:0.5,1);
            \end{axis}
        \end{tikzpicture}
\end{enumerate}
\end{document}

image showing the result of above code

  • Thank you. I appreciate the comments and recommendations on what to use. I think that some of the documentation I was looking at to create the plots to start with was outdated. I'll work on fixing those. – Edd Jun 5 '18 at 23:27
  • You are welcome. To easier find out what could be outdated is the reason why I add a comment line with which version of PGFPlots is used to create the solution. (Please note that there could be a much lower compat level given in the code which is not a contradiction. – Stefan Pinnow Jun 6 '18 at 4:18
  • If the answers helped you to solve your problem, please consider upvoting (by clicking on the arrows next to the score) them and/or marking one of them as the accepted answer (by clicking on the checkmark ✓). Thank you. – Stefan Pinnow Jun 6 '18 at 4:21
1

You can add the option baseline to the tikzpicture environment

/tikz/baseline=<dimension or coordinate or default>

The default is 0pt. In the example below I use the bounding box to align the baseline, and added an extra shift for fine-tuning, you can delete that part

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{cartesian/.append style={axis x line=middle, axis y line=middle, xlabel={$x$}, ylabel={$y$}, axis equal}}
\begin{document}
\begin{enumerate}
  \setcounter{enumi}{16}
\item
  \begin{tikzpicture}[baseline={([yshift=-2pt]current bounding box.north)}]
    \begin{axis}[cartesian, xmin=-3, xmax=3, ymin=-3, ymax=3, xtick={-2,-1,...,2}, ytick={-2,-1,...,2}]
      \addplot [black, mark=*] coordinates {(0,0) (2,1)};
    \end{axis}
  \end{tikzpicture}
\end{enumerate}
\end{document}

enter image description here

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.