Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I've got a piece of TikZ code here and since I am only starting to learn how to use it, I wanted to ask whether this code would be regarded to be ok or rather completely unreadable and what I can do to improve it.

\documentclass{scrartcl}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{patterns}

\tikzset{
every picture/.prefix style={
execute at begin picture=\shorthandoff{"}
}
}

\begin{document}

\begin{tikzpicture}
\newdimen\field
\def\white{white}
\foreach \x in {0, ..., 7}
    \foreach \y in {0, ..., 7} {
        \pgfmathparse{mod(\x+\y,2) ? "white" : "black"}
        \edef\color{\pgfmathresult}
        \pgfmathparse{int(round(56+\x-8*\y))}
        \edef\field{\pgfmathresult}
        \edef\textcolor{\ifx\color\white black\else white\fi}
        \path[fill=\color] (\x, \y) rectangle ++ (1, 1) node[color=\textcolor, pos=.5] {
            \Large
            \ifnum \field < 8
                \pgfmathparse{2^(\field)}
                \pgfmathprintnumber\pgfmathresult
            \else
                \dots
            \fi
        };
    }
\draw (0, 0) -- (0, 8) -- (8, 8) -- (8, 0) -- cycle;
\end{tikzpicture}
\end{document}

The code outputs a demonstration of the Wheat and chessboard problem.

share|improve this question
2  
Don't use \color and \textcolor for the checks, because they are important macros; it's true that the redefinitions are in a group, but one never knows and you might use the original ones in the text, maybe hidden in some macro. –  egreg Apr 8 at 10:52
    
Should this not be on Code Review? –  Raphael Apr 8 at 15:11
    
I think Code Review is there for programming languages that don't have their own site. –  chaosflaws Apr 8 at 15:26
    
@Raphael Whenever I feel the urge to share my Latex code for general feedback rather than to solve a specific problem, I wonder whether Latex code review questions are on topic here. But my sharing and please-criticise-my-code mood usually passes long before I ask the question on meta. –  ThomasH Apr 8 at 16:17

3 Answers 3

up vote 9 down vote accepted

I'd avoid using \color and \textcolor which are important macros in the color and xcolor packages: you might end up in using them, buried in other macros, when typesetting the text of a node.

Also some constructions can be simplified:

\documentclass{scrartcl}

\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{patterns}

\begin{document}

\begin{tikzpicture}
\foreach \x in {0, ..., 7}
  \foreach \y in {0, ..., 7} {
    \ifodd\numexpr\x+\y\relax
      \def\bgcolor{white}\def\fgcolor{black}
    \else
      \def\bgcolor{black}\def\fgcolor{white}
    \fi
  \pgfmathtruncatemacro\field{int(round(56+\x-8*\y))}
  \path[fill=\bgcolor] (\x, \y) rectangle ++ (1, 1) node[color=\fgcolor, pos=.5] {
     \Large
     \ifnum \field < 8
       \pgfmathparse{2^(\field)}%
       \pgfmathprintnumber\pgfmathresult
     \else
       \dots
     \fi
  };
}
\draw (0, 0) -- (0, 8) -- (8, 8) -- (8, 0) -- cycle;
\end{tikzpicture}
\end{document}

Declaring \newdimen\field is wrong in two ways:

  1. the declaration must go outside the loops, or you'd do it at every cycle, wasting a register each time;

  2. you then use \edef\field, so overriding the meaning just given to it as a register name.

enter image description here

share|improve this answer
    
With this, I get "Missing = inserted for \ifnum". It seems that \field is not interpreted as an integer in that context (I ran into that problem earlier). –  chaosflaws Apr 8 at 11:23
    
I removed the shorthandoff declaration as babel is not used in the example. Are you perhaps using Spanish babel? –  egreg Apr 8 at 11:27
    
@egreg: No, German polyglossia. –  chaosflaws Apr 8 at 11:29
    
@percusse: Thanks, that solves it. –  chaosflaws Apr 8 at 11:30
    
@percusse I get an integer, but probably it's better to use that command anyway. –  egreg Apr 8 at 11:30

Here is a very simple TikZ version (I use named styles to choose between black or white) :

enter image description here

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
  square/.style={draw,minimum size=1cm,inner sep=0},
  square 0/.style={square,fill=black},
  square 1/.style={square,fill=white},
  % 
  label/.style={font=\large,align=center,text depth=.3cm,text height=.7cm},
  label 0/.style={label,text=white},
  label 1/.style={label,text=black},
}

\begin{document}
\begin{tikzpicture}
  \foreach \x in {0, ..., 7} {
    \foreach \y in {0, ..., 7} {
      % black or white square
      \pgfmathtruncatemacro\square{mod(\x+\y,2)}
      \node[square \square] at (\x,\y) {};
      % label
      \pgfmathtruncatemacro\field{int(56+\x-8*\y)}
      \ifnum \field < 8 
        \pgfmathtruncatemacro\power{2^(\field)}
      \else%
        \def\power{\dots}%
      \fi%
      \node[label \square]  at (\x,\y) {\power};
    }
  }
\end{tikzpicture}
\end{document}
share|improve this answer

Some number printing fun :) Since there are huge numbers let fpu handle them. I used truncated [.]illionths

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{fpu,babel}%TikZ 3 has a babel library that takes care of active chars
\pgfkeys{
/pgf/number format/mynum/.style={1000 sep={},std=0:3,sci precision=0,
    sci generic={mantissa sep={\!\cdot},exponent={\scriptstyle {10}^{##1}}}
    }
}

\begin{document}
\begin{tikzpicture}
\foreach \x in {0, ..., 7} {
    \foreach \y in {0, ..., 7} {
    \pgfmathsetmacro\mycolor{mod(\x+\y,2) ? 100 : 0}
    \pgfmathtruncatemacro\field{56+\x-8*\y}
    \path[fill=black!\mycolor] (\x, \y) rectangle ++ (1, 1) 
    \pgfextra{\pgfkeys{/pgf/fpu}\pgfmathparse{2^(\field)}\pgfkeys{/pgf/fpu=false}}
        node[text=white!\mycolor!black, midway] {
           \pgfmathprintnumber[mynum]{\pgfmathresult}
        };
    }
}
\draw (0, 0) rectangle (8, 8);
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.