0

I'm coding with Vscode and XeLatex. When running the code below, it worked and cost approximately 20s

\documentclass[border={0pt}]{standalone}

\usepackage{tikz,}

\begin{document}

\begin{tikzpicture}
    [%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        box/.style={rectangle,draw=black,thick, minimum size=28pt},
    ]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\foreach \y in {0,2,4,...,100}{
    \foreach \x in {0,1,...,100}
        \node[box,fill=black] at (\x,\y){};  
}
\end{tikzpicture}

\end{document}

But when it comes to the code like this (which I suppose takes the double computational complexity), it would run for no more than 30s and automatically turned off.


\usepackage{tikz,}

\begin{document}

\begin{tikzpicture}
    [%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        box/.style={rectangle,draw=black,thick, minimum size=28pt},
    ]%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\foreach \y in {0,2,4,...,100}{
    \foreach \x in {0,1,...,100}
        \node[box,fill=black] at (\x,\y){};  
}
\foreach \y in {1,3,5,...,99}{
    \foreach \x in {0,2,...,100}
        \node[box,fill=red] at (\x,\y){};  
}

\end{tikzpicture}

\end{document}

VScode appears this in the first 20s and stop at some point around 25s finally failed

How can I make it run for 20 seconds more to make it, or it just me misunderstood the information fed back by Tex? If there is anything ambiguous please tell me. Thanks for your help!!

2
  • TeX will just run until it's done. In online systems such as Overleaf, there is a 'wrapper' that will kill long jobs, but on a local system, usually you just do that yourself :) – Joseph Wright Mar 12 at 9:58
  • you seem to be running from python, your call to tex from there must be setting a timeout as tex itself will loop forever if that is specified. So you will need to change your python settings somewhere – David Carlisle Mar 12 at 10:13
1

Remarks:

  • Nodes are highly inefficient just for drawing rectangles. There is a rectangle operator just for this job.

  • Using \foreach inside one \path (or \fill, \draw`, ...) is more efficient (file size) than creating a path for each loop iteration.

  • Simple stripes do not need a nested loop.

Code example with much better runtime and file size:

\documentclass[border=0pt, tikz]{standalone}

\begin{document}
\begin{tikzpicture}
\fill[black]
    \foreach \y in {0,2,4,...,100}{
      (0, \y) rectangle +(101, 1)
    }
;
\fill[red]
  \foreach \y in {1,3,5,...,99}{
    \foreach \x in {0,2,...,100}{
      (\x, \y) rectangle +(1, 1)
    }
  }
;
\end{tikzpicture}
\end{document}

The example can be improved further (left as exercise for the reader):

  • First draw the red vertical stripes, then the black horizontal stripes on top of it. This removes the nested loop for the red boxes.

enter image description here

1
  • thanks for your help! :) – dahuang Mar 20 at 9:55

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.