4

I can't access the array elements:

\documentclass{article}
\usepackage{tikz,ifthen}
\usetikzlibrary{calc,patterns}

\def\PatternArray{%
horizontal lines,%
vertical lines,%
north east lines,%
north west lines,%
grid,%
crosshatch,%
dots,%
crosshatch dots,%
fivepointed stars,%
sixpointed stars,%
bricks,%
checkerboard}

\begin{document}

essai

\pgfmathparse{\PatternArray[0]}
\pgfmathresult
\end{document}
5

This works

\documentclass{article}
\usepackage{tikz,ifthen}
\usetikzlibrary{calc,patterns}

\newcommand\PatternArray{{%
  "horizontal lines",%
  "vertical lines",%
  "checkerboard"}}

\begin{document}

\pgfmathparse{\PatternArray[0]}
\pgfmathresult

\pgfmathparse{\PatternArray[2]}
\pgfmathresult

\end{document}

There must be an additional pair of braces around the items; non numeric items should be surrounded by double quotes.

Maybe you'd like this different approach. Indexing starts from 1, but you can also count from the end with negative indices. The input is easier, because leading and trailing spaces are removed.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\definearray}{mm}
 {
  \clist_gclear_new:c { g_tarass_array_#1_clist }
  \clist_gset:cn  { g_tarass_array_#1_clist } { #2 }
 }
\NewExpandableDocumentCommand{\getarrayitem}{mm}
 {
  \clist_item:cn { g_tarass_array_#1_clist } { #2 }
 }
\NewExpandableDocumentCommand{\getarraylength}{m}
 {
  \clist_count:c  { g_tarass_array_#1_clist }
 }
\ExplSyntaxOff

\definearray{Pattern}{
  horizontal lines,
  vertical lines,
  north east lines,
  north west lines,
  grid,
  crosshatch,
  dots,
  crosshatch dots,
  fivepointed stars,
  sixpointed stars,
  bricks,
  checkerboard
}

\begin{document}

\getarraylength{Pattern}

\getarrayitem{Pattern}{1}

\getarrayitem{Pattern}{-2}

\end{document}

enter image description here

  • @Tarass It's a good occasion for learning a much better approach to arrays. – egreg Jan 18 '18 at 9:43

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.