General ⇒ repeat a drawing
repeat a drawing
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
NEW: TikZ book now 40% off at Amazon.com for a short time.
And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p
- Stefan Kottwitz
- Site Admin
- Posts: 10330
- Joined: Mon Mar 10, 2008 9:44 pm
repeat a drawing
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
\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}
Stefan
- Stefan Kottwitz
- Site Admin
- Posts: 10330
- Joined: Mon Mar 10, 2008 9:44 pm
repeat a drawing
- 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
Code, edit and compile here:\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};
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}
- Stefan Kottwitz
- Site Admin
- Posts: 10330
- Joined: Mon Mar 10, 2008 9:44 pm
repeat a drawing
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}
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
Re: repeat a drawing
I am very sorry for the delay in my reply, I had some personal problems that prevented me from working on my drawing.
- Stefan Kottwitz
- Site Admin
- Posts: 10330
- Joined: Mon Mar 10, 2008 9:44 pm
Re: repeat a drawing
nice to read from you again! Great that you can continue your work on the document.
Stefan