Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like to be able to control the width of a pgfplots plot to that of a matrix. The plot should be below the matrix. Ideally the plot's dimension will depend only on the size of the matrix (so if the matrix changes so will the plot).

The plot and matrix are all part of a bigger tikzpicture that includes other components all positioned relative to each other, so I don't want to have two separate tikzpictures. Here's a minimal example code:

\documentclass{minimal}
\usepackage{tikz,pgfplots}

\begin{document}

\begin{tikzpicture}
    \matrix[minimum size=4cm] (A) {
        \node{1};          & \node{2};                      \\
        \draw circle (.1); & \fill (-1,-1) rectangle (1,1); \\
    };
    \begin{axis}
        \addplot[red,domain=0:10,samples=25] {x^2};
    \end{axis}        
\end{tikzpicture}

\end{document}

I've also added a picture of what I'd like it to look like (more or less). Note: I could only achieve that by using two separate tikzpictures...

goal

share|improve this question

1 Answer 1

up vote 4 down vote accepted

You can create your own measurement style via

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\tikzset{getwedist/.code args={#1 and #2}{% measures horizontal distance [#1.west-#2.east] 
  \pgfextra{\pgfpointdiff{\pgfpointanchor{#1}{west}}{\pgfpointanchor{#2}{east}}%
  \xdef\mylength{\the\csname pgf@x\endcsname}}}
}

\begin{document}

\begin{tikzpicture}
    \matrix[minimum size=4cm,append after command={[getwedist=A and A]}] (A) {
        \node{1};          & \node{2};                      \\
        \draw circle (.1); & \fill (-1,-1) rectangle (1,1); \\
    };
    \begin{axis}[width=\mylength,height=2cm,anchor=north,at=(A.south)]
        \addplot[red,domain=0:10,samples=25] {x^2};
    \end{axis}        
\end{tikzpicture}

\end{document}

enter image description here

And please have a look at Why should the minimal class be avoided?

share|improve this answer
    
@12.yakir Related to the minimal class: The MWE package has in fact served me well to replicate images of arbitrary dimensions. :-) If you're bent on using MWE-specific stuff to post questions here. –  1010011010 Jun 17 at 8:06
    
Thanks! I'll stick to article then (or try that MWE package). Your answer worked! –  12.yakir Jun 17 at 8:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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