# Text alignment in a multi-column cell of a tikz matrix

I would like to use a matrix to create a legend in a plot. This matrix contains a cell whose width occupies two columns.

I have a problem with the positioning and alignment of the text of this particular cell.
The text is out of from the frame of the matrix and is not left-aligned, moreover, the text is not well positioned vertically (problem that I can solve manually using \\[...]). text width={...} and align=left don’t seem to work.

Here's my MWE :

\documentclass{standalone}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\usetikzlibrary{fit}

\begin{document}
\begin{tikzpicture}
\begin{semilogyaxis}[domain=0:4, samples=10]
\coordinate (legend) at (axis description cs:1.05,1);
\end{semilogyaxis}
\matrix (matrix1) [draw, matrix of nodes, nodes in empty cells, anchor=north west]
at (legend) {
\ref{graph1}&$x$\\
\ref{graph2}&$x^2$\\%[1.8ex]
&\\%[0.2ex]
\ref{graph-1}&$x^{-1}$\\
\ref{graph-2}&$x^{-2}$\\
};
\node[fit=(matrix1-3-1)(matrix1-3-2),
%text width={5cm},
%align=left
]{%
Bla bla bla bla :%
};
\end{tikzpicture}
\end{document}


# EDIT

This is what I have if I comment text width={...} and align=left :

And this is what I have if I un comment text width={...} and align=left :

The aim would be to have something like:

In this particular example, the goal is easily achievable using a table, but my question is more a general question about tikz matrices.

• Where do you want the text to appear? You have asked for it to appear on the third row of the matrix box, which is what it is doing. – user30471 Sep 6 '20 at 12:07
• @Andrew I edited my question to add screenshots that illustrate my problem. – B Legrand Sep 7 '20 at 9:31

Do you like to have legend in the following format?

For this is not needed matrix7, sufivcient is to use pgfplotsmachinery forlegend:

    legend pos=outer north east, % <---
legend columns = 2,          % <---
legend style = {font=\footnotesize,
style={column sep=0.5em}},
legend cell align=left,      % <---


Complete MWE:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
\begin{tikzpicture}
\begin{semilogyaxis}[
legend pos=outer north east,
legend columns = 2,
legend style = {font=\footnotesize,
style={column sep=0.5em}},
legend cell align=left,
%
domain=0:4,
samples=10
]
\legend{$x$, $x^2$, $x^{-1}$, $x^{-2}$}

• If you have general question about matrix, than you as as such, not that you are going to write a legend with it for tive) \pgfplots diagram.For this pgfplots has (far more effective) own machinery. – Zarko Sep 7 '20 at 9:49