LaTeX forum ⇒ Generalrepeat a drawing

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

repeat a drawing

Postby hany » Mon Dec 07, 2015 4:10 pm

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 70 times

Tags:

User avatar
Stefan Kottwitz
Site Admin
Posts: 8544
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Mon Dec 07, 2015 11:31 pm

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:

  1. \begin{tikzpicture}
  2. \foreach \pos in {0,...,5} {
  3. \node at (1.85*\pos,0) { \tikz\draw [thick,orange,fill=yellow]
  4. (.25,.25) -- (.25,0) -- (.5,0) -- (.5,.25) -- (.75,.25) --
  5. (.75,.5) -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5) --
  6. (0,.5) -- (0,.25) -- (.25,.25) ;} ;}
  7. \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
Site admin

User avatar
Stefan Kottwitz
Site Admin
Posts: 8544
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Mon Dec 07, 2015 11:49 pm

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)
    1. \tikzset{
    2. cross/.pic = {
    3. \draw [thick,orange,fill=yellow]
    4. (.25,.25) -- (.25,0) -- (.5,0) -- (.5,.25) -- (.75,.25) --
    5. (.75,.5) -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5) --
    6. (0,.5) -- (0,.25) -- (.25,.25) ;}
    7. }
  • apply it in any TikZ drawing with a loop command or other commands, such as:

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

Complete example:

  1. \documentclass{article}
  2. \usepackage{tikz}
  3. \begin{document}
  4. \tikzset{
  5. cross/.pic = {
  6. \draw [thick,orange,fill=yellow]
  7. (.25,.25) -- (.25,0) -- (.5,0) -- (.5,.25) -- (.75,.25) --
  8. (.75,.5) -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5) --
  9. (0,.5) -- (0,.25) -- (.25,.25) ;}
  10. }
  11. \tikz \pic foreach \pos in {0,...,5} at (1.85*\pos,0) {cross};
  12. \end{document}


TikZ-pic-example.png
TikZ-pic-example.png (1.63 KiB) Viewed 2832 times


Stefan
Site admin

User avatar
Stefan Kottwitz
Site Admin
Posts: 8544
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Thu Dec 10, 2015 11:38 pm

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.

  1. \documentclass{article}
  2. \usepackage{tikz}
  3. \usetikzlibrary{calc}
  4. \begin{document}
  5. \tikzset{
  6. cross/.pic = {
  7. \draw [thick,orange,fill=yellow]
  8. (.25,.25) -- (.25,0) -- (.5,0) -- (.5,.25) -- (.75,.25) --
  9. (.75,.5) -- (.5,.5) -- (.5,.75) -- (.25,.75) -- (.25,.5) --
  10. (0,.5) -- (0,.25) -- (.25,.25) ;},
  11. formula/.style = {
  12. font = \LARGE\bfseries\sffamily,
  13. color = red,
  14. }
  15. }
  16. \begin{tikzpicture}
  17. \foreach \pos in {0,...,5} {
  18. \pic at (1.85*\pos,0) {cross};
  19. \node[formula] at ($(1.85*\pos,0)+(0.4,0.4)$) {Na};
  20. }
  21. \end{tikzpicture}
  22. \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
Site admin

hany
Posts: 2
Joined: Mon Dec 07, 2015 4:07 pm

Postby hany » Mon Feb 15, 2016 7:26 am

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: 8544
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Mon Feb 15, 2016 7:35 am

Hi Hany,

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

Stefan


Return to “General”

Who is online

Users browsing this forum: No registered users and 2 guests