Generalrepeat a drawing

General information and discussion about TeXnicCenter
Post Reply
hany
Posts: 2
Joined: Mon Dec 07, 2015 4:07 pm

repeat a drawing

Post by hany »

Hello
I want to repeat a drawing in certain distances horizontally to form a group;
then move the whole group in certain distances vertically

such as that on page 1 of the included ions.tex


is there a more simple way, as for example using (for each), to make it look like that on pages 5-9 of the included ions.tex

How will I view your answer

Thank you
Attachments
Ions.zip
(42.39 KiB) Downloaded 420 times

Recommended reading 2024:

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

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

User avatar
Stefan Kottwitz
Site Admin
Posts: 10319
Joined: Mon Mar 10, 2008 9:44 pm

repeat a drawing

Post by Stefan Kottwitz »

Hello Hany,

welcome to the forum!

There are several possibilities. I can show some, perhaps I start with just one, maybe Johannes might add another or I do.
  • classic macros storing a series of TikZ commands
  • \savebox for a TikZ picture, then \usebox to print this box as copies several times
  • a TikZ \foreach loop
  • TikZ pic objects
Since the first two are classic LaTeX, I show a TikZ \foreach loop solution:

Code: Select all

\begin{tikzpicture}
  \foreach \pos in {0,...,5} {
    \node at (1.85*\pos,0) { \tikz\draw [thick,orange,fill=yellow]
      (.25,.25) -- (.25,0) -- (.5,0)   -- (.5,.25)  -- (.75,.25) --
      (.75,.5)  -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5)  --
      (0,.5)    -- (0,.25) -- (.25,.25) ;} ;}
\end{tikzpicture}
Here, I printed nodes in a loop. I simply placed TikZ pictures in the node - no problem. So I could keep the absolute coordinates here.

Stefan
LaTeX.org admin
User avatar
Stefan Kottwitz
Site Admin
Posts: 10319
Joined: Mon Mar 10, 2008 9:44 pm

repeat a drawing

Post by Stefan Kottwitz »

Now I wrote a pic solution:
  • globally define a pic style with a name, which you can use any time you want (well you could do it locally as well)

    Code: Select all

    \tikzset{
      cross/.pic = {
        \draw [thick,orange,fill=yellow]
          (.25,.25) -- (.25,0) -- (.5,0)   -- (.5,.25)  -- (.75,.25) --
          (.75,.5)  -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5)  --
          (0,.5)    -- (0,.25) -- (.25,.25) ;}
    }
  • apply it in any TikZ drawing with a loop command or other commands, such as:

    Code: Select all

    \tikz \pic foreach \pos in {0,...,5} at (1.85*\pos,0) {cross};
Complete example:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikzset{
  cross/.pic = {
    \draw [thick,orange,fill=yellow]
      (.25,.25) -- (.25,0) -- (.5,0)   -- (.5,.25)  -- (.75,.25) --
      (.75,.5)  -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5)  --
      (0,.5)    -- (0,.25) -- (.25,.25) ;}
}
\tikz \pic foreach \pos in {0,...,5} at (1.85*\pos,0) {cross};
\end{document}
TikZ-pic-example.png
TikZ-pic-example.png (1.63 KiB) Viewed 15969 times
Stefan
LaTeX.org admin
User avatar
Stefan Kottwitz
Site Admin
Posts: 10319
Joined: Mon Mar 10, 2008 9:44 pm

repeat a drawing

Post by Stefan Kottwitz »

Here is an example showing how to place also the text node. It could be part of the pic, or separate. I made it separate here.

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\tikzset{
  cross/.pic = {
    \draw [thick,orange,fill=yellow]
      (.25,.25) -- (.25,0) -- (.5,0)   -- (.5,.25)  -- (.75,.25) --
      (.75,.5)  -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5)  --
      (0,.5)    -- (0,.25) -- (.25,.25) ;},
  formula/.style = {
    font  = \LARGE\bfseries\sffamily,
    color = red,
  }
}
\begin{tikzpicture}
  \foreach \pos in {0,...,5} {
    \pic at (1.85*\pos,0) {cross};
    \node[formula] at ($(1.85*\pos,0)+(0.4,0.4)$) {Na};
  }
\end{tikzpicture}
\end{document}
I used the calc library syntax to add an offset (0.4,0.4) to the node position, so you need to load calc as above. I always use styles for formatting in nodes.

Stefan
LaTeX.org admin
hany
Posts: 2
Joined: Mon Dec 07, 2015 4:07 pm

Re: repeat a drawing

Post by hany »

Thank you Stefan very much.
I am very sorry for the delay in my reply, I had some personal problems that prevented me from working on my drawing.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10319
Joined: Mon Mar 10, 2008 9:44 pm

Re: repeat a drawing

Post by Stefan Kottwitz »

Hi Hany,

nice to read from you again! Great that you can continue your work on the document.

Stefan
Post Reply