[Risolto] TikZ e matrici

  • Creatore
    Topic
  • #67133
    Up
    0
    Down
    ::


    Salve,

    come da oggetto, vorrei creare una matrice e colorarne lo sfondo non completamente, ma solo per una riga ed una colonna. Nel pgf manual ho capito che:

    `
    \matrix [nodes={fill=blue!20,minimum size=5mm}]{

    };
    `
    permette di colorare lo sfondo completamente, mentre il codice:

    `
    \begin{tikzpicture}
    [row 1/.style={red},
    column 2/.style={green!50!black},
    row 3 column 3/.style={blue}]
    \matrix{

    };
    \end{tikzpicture}
    `
    colora i nodi. Ho, ovviamente, provato alcune varianti:

    `
    \begin{tikzpicture}
    [row 1/.style={fill=red!50},
    column 2/.style={fill=red!50}]
    \matrix{

    };
    \end{tikzpicture}
    `
    e

    `
    \matrix [row 1/.style={fill=blue!20}]{

    };
    `
    ma, sebbene non abbia errori di compilazione, non ottengo il risultato voluto. Qualcuno ha già affrontato questo tipo di problema?

    Ciao
    Claudio

Visualizzazione 10 filoni di risposte
  • Autore
    Risposte
    • #67134
      Up
      0
      Down
      ::


      Per chi avesse lo stesso problema, ho risolto in questo modo:

      `
      \begin{tikzpicture}
      \tikzset{mycolor/.style = {opacity=.4,line width=4 mm,line cap=round,color=#1}}
      \matrix[matrix of math nodes,left delimiter=(,right delimiter=)]
      {
      \node(a) {0}; & \node{1}; & \node {0}; &\node {0};\\
      \node {0}; & \node{1}; & \node {1}; &\node {1};\\
      \node(c) {1}; & \node{1}; & \node {1}; &\node (d){0};\\
      \node(b) {0}; & \node{1}; & \node {0}; &\node {0};\\
      };
      \draw[mycolor=red!45] (a.north)–(b.south);
      \draw[mycolor=red!45] (c.west)–(d.east);
      \end{tikzpicture}
      `

    • #67135
      Liverpool
      Partecipante
        Up
        0
        Down
        ::


        Non so se ti interessa ancora, ma andando moooolto per tentativi, questo codice sembra funzionare:
        `
        [row 1/.style={every node/.style={fill=red!50}},
        column 2/.style={every node/.style={fill=red!50}}]`
        Ciao

      • #67136
        Up
        0
        Down
        ::

        Liverpool” post=66112Non so se ti interessa ancora, ma andando moooolto per tentativi, questo codice sembra funzionare:
        `
        [row 1/.style={every node/.style={fill=red!50}},
        column 2/.style={every node/.style={fill=red!50}}]`
        Ciao

        Grazie mille: funziona anche questo :)!

        Ciao
        Claudio

      • #67137
        Liverpool
        Partecipante
          Up
          0
          Down
          ::


          Che ne dici di questa modifica al tuo codice?
          `
          \documentclass{article}
          \usepackage{tikz}
          \usetikzlibrary{matrix,backgrounds}
          \begin{document}
          \begin{tikzpicture}
          \tikzset{mycolor/.style = {opacity=.4,line width=4 mm,line cap=round,color=#1}}
          \matrix[matrix of math nodes,left delimiter=(,right delimiter=)]
          {
          \node(a) {0}; & \node{1}; & \node {0}; &\node {0};\\
          \node {0}; & \node{1}; & \node {1}; &\node {1};\\
          \node(c) {1}; & \node{1}; & \node {1}; &\node (d){0};\\
          \node(b) {0}; & \node{1}; & \node {0}; &\node {0};\\
          };

          \begin{pgfonlayer}{background}
          \draw[mycolor=red!45] (a.north)–(b.south);
          \draw[mycolor=red!45] (c.west)–(d.east);
          \end{pgfonlayer}
          \end{tikzpicture}
          \end{document}`
          N.B.: richiede \usetikzlibrary{backgrounds} nel preambolo

          Ciao

        • #67138
          Up
          0
          Down
          ::

          Liverpool” post=66118Che ne dici di questa modifica al tuo codice?
          `
          \documentclass{article}
          \usepackage{tikz}
          \usetikzlibrary{matrix,backgrounds}
          \begin{document}
          \begin{tikzpicture}
          \tikzset{mycolor/.style = {opacity=.4,line width=4 mm,line cap=round,color=#1}}
          \matrix[matrix of math nodes,left delimiter=(,right delimiter=)]
          {
          \node(a) {0}; & \node{1}; & \node {0}; &\node {0};\\
          \node {0}; & \node{1}; & \node {1}; &\node {1};\\
          \node(c) {1}; & \node{1}; & \node {1}; &\node (d){0};\\
          \node(b) {0}; & \node{1}; & \node {0}; &\node {0};\\
          };

          \begin{pgfonlayer}{background}
          \draw[mycolor=red!45] (a.north)–(b.south);
          \draw[mycolor=red!45] (c.west)–(d.east);
          \end{pgfonlayer}
          \end{tikzpicture}
          \end{document}`
          N.B.: richiede \usetikzlibrary{backgrounds} nel preambolo

          Ciao

          Dico che è decisamente migliore, grazie :smile:. La leggibilità aumenta un sacco.

          Ciao
          Claudio

        • #67139
          Up
          0
          Down
          ::


          A beneficio di chi avesse bisogno, per colorare invece soltanto lo sfondo di alcuni elementi ho utilizzato questo codice (sempre seguendo il metodo con pgfonlayer):

          `
          \begin{tikzpicture}
          \tikzset{myfillcolor/.style = {opacity=.4,circle, fill=#1, draw=none}}
          \matrix[matrix of math nodes,left delimiter=(,right delimiter=)]
          {
          \node(h) {0}; & \node(q){0.4};& \node {0.2}; & \node(a){0.4}; \\
          \node {0.2}; & \node(w){0.4};& \node {0}; & \node(b){0.2}; \\
          \node {0.4}; & \node(r){0.2}; &\node(t) {0.4}; & \node(c){0.2}; \\
          \node(g) {0.2}; & \node(f){0};& \node(e) {0.2}; & \node(d){0.4}; \\
          };
          %——————————————————-
          \begin{pgfonlayer}{background}
          \foreach \x in {q,w,r,t,d}
          \draw[myfillcolor=red!45] (\x.south west)rectangle (\x.north east);
          \end{pgfonlayer}
          %——————————————————-
          \foreach \x/\xtext in {a/1,b/0.8,c/1,d/0.8}
          \node[shift=(\x.east),xshift=0.75cm](0,0) {\xtext} ;
          %——————————————————-
          \foreach \x/\xtext in {g/0.8,f/1,e/0.8,d/1}
          \node[shift=(\x.south),yshift=-0.45cm](0,0) {\xtext} ;
          \end{tikzpicture}
          `
          Il risultato grafico è:

          [attachment=97]matrix.jpg[/attachment]

          Attachments:
          You must be logged in to view attached files.
        • #67140
          Up
          0
          Down
          ::


          Ciao a tutti,
          stavo cercando di modificare il codice riportato sopra per selezionare due colonne adiacenti di una matrice… non ci sono riuscito, il rettangolo infatti non prende totalmente i nodi che interessa prendere e quindi vi chiedo aiuto

          `

          \usetikzlibrary{matrix,backgrounds}

          \begin{tikzpicture}
          \tikzset{mycolor/.style = {opacity=.4,rounded corners=2mm, fill=#1}}
          \matrix[matrix of math nodes,left delimiter={[},right delimiter={]} ]
          {
          \node {1}; & \node(a) {-1}; & \node {-1}; & \node {1};\\
          \node {1}; & \node {1}; & \node {-1}; & \node {-1};\\
          \node {1}; & \node {-1}; & \node {1}; & \node {-1};\\
          \node {1}; & \node {1}; & \node {1}; & \node {1};\\
          \node {\vdots}; & \node {\vdots}; & \node {\vdots}; & \node {\vdots};\\
          \node {1}; & \node {-1}; & \node {-1}; &\node {1};\\
          \node {\vdots}; & \node {\vdots}; & \node {\vdots}; & \node {\vdots};\\
          \node {1}; & \node {1}; & \node(b) {1}; &\node {1};\\
          };

          \begin{pgfonlayer}{background}
          \draw[mycolor=black!10] (a.north) rectangle (b.south);
          \end{pgfonlayer}
          \end{tikzpicture}`

          Come sempre, grazie.

        • #67141
          Up
          0
          Down
          ::


          `\draw[mycolor=black!10] (a.north west) rectangle (b.south east);`
          Ma devi anche allargare un po’ il nodo (b):
          `\node(b) {\ 1\ }`
          Suppongo che ci siano metodi migliori che farlo così a occhio, ma funziona.
          Ciao
          Enrico

        • #67142
          Up
          0
          Down
          ::


          Perfetto funziona egregiamente. Grazie.
          Ma si possono allineare matrici matematiche in tikz di modo da creare un’equazione?
          Io stavo provando con
          `\begin{equation}
          \begin{bmatrix}
          Y_{11}\\
          Y_{21}\\
          Y_{31}\\
          Y_{41}\\
          Y_{12}\\
          \vdots\\
          Y_{13}\\
          \vdots\\
          Y_{43}
          \end{bmatrix}
          =
          \begin{tikzpicture}
          \tikzset{mycolor/.style = {opacity=.4,rounded corners=2mm, fill=#1}}
          \matrix[matrix of math nodes, left delimiter={[},right delimiter={]} ] at (0,-20)
          {
          \node {1}; & \node(a) {-1}; & \node {-1}; & \node {1};\\
          \node {1}; & \node {1}; & \node {-1}; & \node {-1};\\
          \node {1}; & \node {-1}; & \node {1}; & \node {-1};\\
          \node {1}; & \node {1}; & \node {1}; & \node {1};\\
          \node {\vdots}; & \node {\vdots}; & \node {\vdots}; & \node {\vdots};\\
          \node {1}; & \node {-1}; & \node {-1}; &\node {1};\\
          \node {\vdots}; & \node {\vdots}; & \node {\vdots}; & \node {\vdots};\\
          \node {1}; & \node {1}; & \node(b) {\ 1\ }; &\node {1};\\
          };
          \begin{pgfonlayer}{background}
          \draw[mycolor=black!10] (a.north west) rectangle (b.south east);
          \end{pgfonlayer}
          \end{tikzpicture}
          \begin{bmatrix}
          \mu\\
          \tau\\
          \delta\\
          \tau\delta
          \end{bmatrix}
          +
          \begin{bmatrix}
          \epsilon_{11}\\
          \epsilon_{21}\\
          \epsilon_{31}\\
          \epsilon_{41}\\
          \epsilon_{12}\\
          \vdots\\
          \epsilon_{13}\\
          \vdots\\
          \epsilon_{43}
          \end{bmatrix}
          \label{eq:matriceexp}
          \end{equation}`
          ma credo di essere molto lontano dalla soluzione: ho mischiato tikz e bmatrix perché usando solo matrici composte con tikz finiva che mi si sovrapponevano tutte al centro della pagina.

        • #67143
          Up
          0
          Down
          ::


          Puoi impostare la tikzpicture in modo opportuno:
          `\begin{tikzpicture}[baseline=(current bounding box.center)]`
          Ma il risultato non è ottimo, perché le spaziature sono diverse fra una matrice e l’altra.

          C’è un altro metodo: scrivi questo nel preambolo
          `\usetikzlibrary{calc}
          \newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
          \newcommand{\DrawBox}[1][]{%
          \tikz[overlay,remember picture]{
          \draw[opacity=.4,rounded corners=2mm, fill=#1]
          ($(left)+(-0.2em,0.9em)$) rectangle
          ($(right)+(1.2em,-0.4em)$);}
          }`
          e poi la tua matrice può diventare una normale bmatrix:
          `\begin{equation}
          \begin{bmatrix}
          Y_{11}\\
          Y_{21}\\
          Y_{31}\\
          Y_{41}\\
          Y_{12}\\
          \vdots\\
          Y_{13}\\
          \vdots\\
          Y_{43}
          \end{bmatrix}
          =
          \begin{bmatrix}
          1 & \tikzmark{left} -1 & -1 & 1\\
          1 & 1 & -1 & -1\\
          1 & -1 & 1 & -1\\
          1 & 1 & 1 & 1\\
          \vdots & \vdots & \vdots & \vdots\\
          1 & -1 & -1 &1\\
          \vdots & \vdots & \vdots & \vdots\\
          1 & 1 & \tikzmark{right} 1 & 1\\
          \end{bmatrix}\DrawBox[black!10]
          \end{equation}`
          Aggiusta tu i parametri necessari.

          Ciao
          Enrico

        • #67144
          Up
          0
          Down
          ::


          Ah, però, carina questa via.
          Grazie mille.

      Visualizzazione 10 filoni di risposte
      • Devi essere connesso per rispondere a questo topic.

      Go to top