2

I am a beginner to Latex and Tikz. I'm facing the following problem: I want to create a table with hex values from 00 to FF in tikz using for each. However, is there a way to specify hex values with other variables? Currently, the example in TikZ manual uses the existing variables in the loop like this:

\begin{tikzpicture}
\foreach \x in {0,1,2,3}
\foreach \y in {0,1,2,3}
{
\draw (\x,\y) circle (0.2cm);
\fill (\x,\y) circle (0.1cm);
}
\end{tikzpicture}

It uses the x and y variables, but how do I specify my own variable, with hex values for each node?

1
  • You should look up how to use counters, and then perhaps use the moreenum package to convert counter values to hex.
    – Seamus
    Commented Dec 11, 2016 at 16:53

1 Answer 1

5

I'd suggest keeping yourself and TeX in decimal, and converting to hex as needed for presentation:

enter image description here

\documentclass{standalone}
\usepackage{fmtcount} % for hex conversion
\newcounter{somevalue}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\foreach \row in {0, ..., 15}
{
  \foreach \col in {0, ..., 15}
  {
    \node[xshift=\col cm, yshift=-\row em]
      {0x\padzeroes[2]\Hexadecimalnum{\thesomevalue}};
    \stepcounter{somevalue}
  };
}
\end{tikzpicture}

\end{document}
5
  • 2
    pgfmath knows about hex numbers hex(x) and Hex(x) can do the conversion on integers
    – percusse
    Commented Dec 11, 2016 at 17:27
  • Thanks for your answer. When I add draw to the \node, the table is not rendered properly. Could we use grid for a proper table? I cannot make the borders overlap correctly with xshift and yshift.
    – Tu Do
    Commented Dec 11, 2016 at 17:55
  • Not knowing what you want to draw, I can't say. The usual tabular designs tend to not add grids or excess lines. Commented Dec 12, 2016 at 3:48
  • Well, what if I want to draw something similar to the periodic table? Those tables tend to have grid, but width big square boxes .
    – Tu Do
    Commented Dec 12, 2016 at 5:14
  • At 16x16 on a normal portrait sheet of paper and typical font sizes, those boxes won't be large, but I'll see what I can come up with if someone doesn't beat me to it. Commented Dec 12, 2016 at 13:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .