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 437 times
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
- Stefan Kottwitz
- Site Admin
- Posts: 10360
- 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: 10360
- 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
\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: 10360
- 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: 10360
- 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