Math & ScienceDefining terms with reference to others

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
Alec W
Posts: 2
Joined: Sun May 13, 2012 3:15 pm

Defining terms with reference to others

Post by Alec W »

Hi,

I've just found Latex recently & have fallen in love with its precision. As a maths teacher I am looking to set up various general routines that I can just copy & paste into the tests that I write.

I wanted to write a general routine for making isometric dotty paper where I could simply define the size & spacing using some opening definitions & a scale command. After stumbling around for an elegant solution I ended up doing this, which works, but I wondered if anyone could advise me as to a nicer way to do it. Is it possible to define the \innerx and \innery with reference to the \outerx - 1 & \outery - 1?

This is my script

Code: Select all

\begin{tikzpicture} [scale=0.75]

\def\outery{20}
\def\outerx{10}
\def\innerx{9}
\def\innery{19}
\def\costhirty{0.8660254cm}
\def\twocosthirty{1.732050808}

	\foreach [evaluate=\a using (\twocosthirty*\b)] \b in {0,1,...,\outerx}
		\foreach \c in {0,1,...,\outery}
		\filldraw (\a,\c) circle (0.05cm) ;
	\foreach [evaluate=\d using (\twocosthirty*\e)] \e in {0,1,...,\innerx}
		\foreach \f in {0,1,...,\innery}
		\filldraw [xshift=\costhirty,yshift=0.5cm] (\d,\f) circle (0.05cm) ;
	
\end{tikzpicture}
Alec

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Defining terms with reference to others

Post by cgnieder »

Maybe something like this:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usepackage{etex}
% the etex package provides \loccount which enables us to
% locally define \count's

\begin{document}

\begin{tikzpicture}[scale=0.75]
  \loccount\X \loccount\Y
  \X=10 \Y=20
  \def\costhirty{0.8660254}
  \foreach [evaluate=\a using (2*\costhirty*\b)] \b in {0,1,...,\X}
    \foreach \c in {0,1,...,\Y}
      \filldraw (\a,\c) circle (0.05cm) ;
  \advance \X by -1 \advance \Y by -1
  \foreach [evaluate=\d using (2*\costhirty*\e)] \e in {0,1,...,\X}
    \foreach \f in {0,1,...,\Y}
      \filldraw [xshift=\costhirty cm,yshift=0.5cm] (\d,\f) circle (0.05cm) ;
\end{tikzpicture}

\end{document}
I use counts here which in general should only be defined only once in a document. The etex package however provides \loccount so they will stay local and could be newly defined inside other groups or environments in the same document.

If you don't like using another package and you know you will define(*) \X and \Y only once, you can replace \loccount with \newcount and leave out \usepackage{etex}.


(*) I.e., saying \newcount\X. You can use \X=<num> as many times as you like...

Regards
site moderator & package author
Alec W
Posts: 2
Joined: Sun May 13, 2012 3:15 pm

Re: Defining terms with reference to others

Post by Alec W »

Many thanks, that's exactly the sort of thing I was looking for.

Alec
Post Reply