Re: Le mie (dis)avventure con TikZ

#96714
GKT
Partecipante
    Up
    0
    Down
    ::

    lorenzo.pantieri” post=96913Carissimi, l’uso che faccio del formidabile TikZ è molto elementare.
    `\documentclass{article}
    \usepackage{tikz}

    \usetikzlibrary{matrix,patterns}

    \begin{document}

    \begin{center}
    \begin{tikzpicture}[x=5mm,y=5mm]
    \matrix (a)[matrix of nodes, nodes in empty cells,nodes={ text width=8mm, text depth=1mm, text centered}]{
    & $1$ & $-3$ & $-3$ & $-4$ \\
    $4$ & & $4$ & $4$ & $4$ \\
    & $1$ & $1$ & $1$ & $0$ \\
    };
    \begin{scope}[blue]
    \draw (a-1-2.north west)–(a-3-1.south east);
    \draw (a-2-1.south west)–(a-2-5.south east);
    \draw (a-1-4.north east)–(a-3-4.south east);
    \end{scope}
    \end{tikzpicture}
    \end{center}

    \begin{center}
    \begin{tikzpicture}[font=\small,x=10mm, y=10mm]

    \draw [->] (0,0) — (8,0) node [below right] () {$r$};

    \foreach \x in {2,4,6}{
    \draw (\x,3pt)–(\x,-3pt);

    \begin{scope} [dotted]
    \draw (\x,0) — (\x,-3.5);
    \end{scope}}

    \node

    at (0,-0.5) {$F_1$};
    \node

    at (0,-1) {$F_2$};
    \node

    at (0,-1.5) {$F_3$};
    \node

    at (0,-2.5) {$P$};
    \node

    at (0,-3.0) {$\mathcal{S}$};

    \node [above] at (2,0) {$-3$};
    \node [above] at (4,0) {$0$};
    \node [above] at (6,0) {$3$};

    \node at (1,-0.5) {$-$};
    \node at (3,-0.5) {$-$};
    \node at (5,-0.5) {$+$};
    \node at (7,-0.5) {$+$};

    \node at (1,-1) {$-$};
    \node at (3,-1) {$-$};
    \node at (5,-1) {$-$};
    \node at (7,-1) {$+$};

    \node at (1,-1.5) {$-$};
    \node at (3,-1.5) {$+$};
    \node at (5,-1.5) {$+$};
    \node at (7,-1.5) {$+$};

    \node at (1,-2.5) {$-$};
    \node at (3,-2.5) {$+$};
    \node at (5,-2.5) {$-$};
    \node at (7,-2.5) {$+$};

    \draw [fill] (4,-0.5) circle (2.0pt);
    \draw [fill] (6,-1) circle (2.0pt);
    \draw [fill] (2,-1.5) circle (2.0pt);

    \draw [fill] (2,-2.5) circle (2.0pt);
    \draw [fill] (4,-2.5) circle (2.0pt);
    \draw [fill] (6,-2.5) circle (2.0pt);

    \pattern [pattern= north east lines, pattern color=blue!75!white] (0,-3) rectangle (2,-3.5);
    \pattern [pattern= north east lines, pattern color=blue!75!white] (4,-3) rectangle (6,-3.5);

    \draw [blue,ultra thick] (0,-3.0) — (2,-3.0);
    \draw [blue,ultra thick] (4,-3.0) — (6,-3.0);
    \draw [blue,fill=white,very thick] (2,-3.0) circle (2.0pt);
    \draw [blue,fill=white,very thick] (4,-3.0) circle (2.0pt);
    \draw [blue,fill=white,very thick] (6,-3.0) circle (2.0pt);
    \end{tikzpicture}
    \end{center}

    \end{document}`
    Vorrei migliorare i risultati precedenti: quello non non va bene è l’incolonnamento dei numeri nella prima tabella, e delle lettere nella seconda. Nel primo caso vorrei che i numeri fossero allineanti a destra (e non a sinistra), nel secondo caso vorrei che le lettere fossero centrate verticalmente.

    Come fare?

    Ah, più la soluzione è semplice, più la apprezzo!! 😉

    Ti piace così?`\documentclass{article}

    \usepackage{tikz}

    \usetikzlibrary{matrix,patterns}

    \begin{document}

    \begin{center}
    \begin{tikzpicture}[x=5mm,y=5mm]
    \matrix (a)[matrix of nodes, nodes in empty cells,nodes={text width=8mm, text depth=1mm,text ragged left}]{
    & $1$ & $-3$ & $-3$ & $-4$ \\
    $4$ & & $4$ & $4$ & $4$ \\
    & $1$ & $1$ & $1$ & $0$ \\
    };
    \begin{scope}[blue]
    \draw (a-1-2.north west)–(a-3-1.south east);
    \draw (a-2-1.south west)–(a-2-5.south east);
    \draw (a-1-4.north east)–(a-3-4.south east);
    \end{scope}
    \end{tikzpicture}
    \end{center}

    \begin{center}
    \begin{tikzpicture}[font=\small,x=10mm, y=10mm]

    \draw [->] (0,0) — (8,0) node [below right] () {$r$};

    \foreach \x in {2,4,6}{
    \draw (\x,3pt)–(\x,-3pt);

    \begin{scope} [dotted]
    \draw (\x,0) — (\x,-3.5);
    \end{scope}}

    \node at (-0.25,-0.5) {$F_1$};
    \node at (-0.25,-1) {$F_2$};
    \node at (-0.25,-1.5) {$F_3$};
    \node at (-0.25,-2.5) {$P$};
    \node at (-0.25,-3.0) {$\mathcal{S}$};

    \node [above] at (2,0) {$-3$};
    \node [above] at (4,0) {$0$};
    \node [above] at (6,0) {$3$};

    \node at (1,-0.5) {$-$};
    \node at (3,-0.5) {$-$};
    \node at (5,-0.5) {$+$};
    \node at (7,-0.5) {$+$};

    \node at (1,-1) {$-$};
    \node at (3,-1) {$-$};
    \node at (5,-1) {$-$};
    \node at (7,-1) {$+$};

    \node at (1,-1.5) {$-$};
    \node at (3,-1.5) {$+$};
    \node at (5,-1.5) {$+$};
    \node at (7,-1.5) {$+$};

    \node at (1,-2.5) {$-$};
    \node at (3,-2.5) {$+$};
    \node at (5,-2.5) {$-$};
    \node at (7,-2.5) {$+$};

    \draw [fill] (4,-0.5) circle (2.0pt);
    \draw [fill] (6,-1) circle (2.0pt);
    \draw [fill] (2,-1.5) circle (2.0pt);

    \draw [fill] (2,-2.5) circle (2.0pt);
    \draw [fill] (4,-2.5) circle (2.0pt);
    \draw [fill] (6,-2.5) circle (2.0pt);

    \pattern [pattern= north east lines, pattern color=blue!75!white] (0,-3) rectangle (2,-3.5);
    \pattern [pattern= north east lines, pattern color=blue!75!white] (4,-3) rectangle (6,-3.5);

    \draw [blue,ultra thick] (0,-3.0) — (2,-3.0);
    \draw [blue,ultra thick] (4,-3.0) — (6,-3.0);
    \draw [blue,fill=white,very thick] (2,-3.0) circle (2.0pt);
    \draw [blue,fill=white,very thick] (4,-3.0) circle (2.0pt);
    \draw [blue,fill=white,very thick] (6,-3.0) circle (2.0pt);
    \end{tikzpicture}
    \end{center}

    \end{document}`Ovviamente il codice si può migliorare dal punto di vista dell’eleganza, ma forse prima è meglio sapere se il risultato è accettabile…

    Lorenzo

    Go to top