4

I am using the following to create a small graph. I would like to scale the size, but the edges drawn within the foreach do not scale.

\begin{tikzpicture}[scale=0.5] % problem <---
        \tikzstyle{every node}=[draw,circle,fill=black,minimum size=4pt,
                                inner sep=0pt]
        \node (a) at (0,0) {};
        \node (b) at (0,1) {};
        \node (c) at (0,2) {};
        \node (d) at (0,3) {};
        \node (e) at (1,1) {};
        \node (f) at (1,2) {};

        \tikz\foreach \x in {0,1,2,3}
                \foreach \y in {1,2}
                    \draw (0, \x) -- (1, \y);
    \end{tikzpicture}

Problematically, when set scale=0.5, only the nodes scale down and not the lines drawn using the nested foreach.

enter image description here

What I would expect is a smaller version of the following image (produced without setting scale). How can I accomplish this? My graph will get quite large, so it is important to preserve the ability to use foreach loop.

enter image description here

2
  • 2
    Don't use \tikz in \tikzpicture environment.
    – user31034
    Feb 24 at 0:03
  • 1
    @HeatherGuarnera since you are already in a tikz environment there is no need to use \tikz command again for the foreach -- have a look athe answer below if it suits the requirement
    – js bibra
    Feb 24 at 0:22
3

enter image description here

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}[scale=0.5] % problem <---
        \tikzstyle{every node}=[draw,circle,fill=black,minimum size=4pt,
        inner sep=0pt]
        \node (a) at (0,0) {};
        \node (b) at (0,1) {};
        \node (c) at (0,2) {};
        \node (d) at (0,3) {};
        \node (e) at (1,1) {};
        \node (f) at (1,2) {};
        
        \foreach \x in {0,1,2,3}
        \foreach \y in {1,2}
        \draw (0, \x) -- (1, \y);
    \end{tikzpicture}

    \begin{tikzpicture}[scale=1.5] % problem <---
    \tikzstyle{every node}=[draw,circle,fill=black,minimum size=4pt,
    inner sep=0pt]
    \node (a) at (0,0) {};
    \node (b) at (0,1) {};
    \node (c) at (0,2) {};
    \node (d) at (0,3) {};
    \node (e) at (1,1) {};
    \node (f) at (1,2) {};
    
    \foreach \x in {0,1,2,3}
    \foreach \y in {1,2}
    \draw (0, \x) -- (1, \y);
\end{tikzpicture}
\end{document}
1
  • 2
    good answer, +1. Also, if you find it worth answering OP's question, you should give +1 to the question.
    – user31034
    Feb 24 at 2:26
2

Just shorter code and correct syntax for defining nodes style. For exercise.

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[scale=0.5]% <--- scaling is not problem 
\tikzset{every node/.style = {circle, fill=black, minimum size=4pt, % <---
                              inner sep=0pt, outer sep=0pt}}
\foreach \i in {0,1,2,3}
    \foreach \j in {1,2}
{
\draw[red]   (0,\i) node {} -- (1,\j) node {};
}
    \end{tikzpicture}
\end{document}

enter image description here

Edit: In the case, if like to have named nodes, just add names to nodes. For example:

\documentclass[border=3.141592]{standalone}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[scale=0.5]% <--- scaling is not problem 
\tikzset{
    every node/.style = {circle, fill=black, minimum size=4pt, 
                         inner sep=0pt, outer sep=0pt}
        }
\foreach \i in {0,1,2,3}
    \foreach \j in {1,2}
{
\draw[red]   (0,\i) node (a\i) {} -- (1,\j) node (b\j) {};
}
\draw[blue] (a0) -- (a1);
    \end{tikzpicture}
\end{document}

enter image description here

3
  • How would you name each node recursively? For instace if I want to call the nodes on yhe left side i1... i4 and on the rght side j1 to j2. Asking out of curiosity :)
    – Jes
    Feb 24 at 7:19
  • 1
    @Jes, just ad node names: \draw[red] (0,\i) node (n0\i) {} -- (1,\j) node (n1\j) {}; See edit in answer.
    – Zarko
    Feb 24 at 8:54
  • thank you very much. I did not think about doing that :D
    – Jes
    Feb 24 at 15:16

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.