![我怎样才能减少这些 tikz 节点的重复代码?](https://linux22.com/image/264204/%E6%88%91%E6%80%8E%E6%A0%B7%E6%89%8D%E8%83%BD%E5%87%8F%E5%B0%91%E8%BF%99%E4%BA%9B%20tikz%20%E8%8A%82%E7%82%B9%E7%9A%84%E9%87%8D%E5%A4%8D%E4%BB%A3%E7%A0%81%EF%BC%9F.png)
这与早期的问题我得到了第一部分的答案,并点击了第二部分的某些链接,最后写出了一堆混杂的代码来实现我想要的效果,但是我不太明白 for 循环示例中的内容,所以它们对我来说不是最好的起点。
以下是圆圈代码的一个小例子
\documentclass[paper=a4, fontsize=12pt]{scrartcl}
\usepackage{amsmath,amsfonts,amsthm}
\usepackage{graphicx}
\usepackage{float}
\setlength\parindent{0pt}
\usepackage{tikz}
\usetikzlibrary{arrows,%
plotmarks}
\begin{document}
\begin{figure}[H]
\makeatletter
\tikzset{
dot diameter/.store in=\dot@diameter,
dot diameter=3pt,
dot spacing/.store in=\dot@spacing,
dot spacing=10pt,
dots/.style={
line width=\dot@diameter,
line cap=round,
dash pattern=on 0pt off \dot@spacing
}
}
\makeatother
\centering
\label{fig:circle}
\begin{tikzpicture}
% Axis
\draw[thick,->,black] (-4,0)--(4,0) node[below] {$n_x$};
\draw[thick,->,black] (0,-4)--(0,4) node[left] {$n_y$};
\draw[black,thick] (0,0) circle (3.5cm);
%dots
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (0pt,3.5) -- (0pt,6pt) ;
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (0pt,0) -- (110pt,0) ;
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (10pt,3.5) -- (10pt,6pt) ;
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (20pt,3.5) -- (20pt,6pt) ;
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (30pt,3.5) -- (30pt,6pt) ;
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (40pt,3.5) -- (40pt,6pt);
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (50pt,3.5) -- (50pt,6pt) ;
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (60pt,3.5) -- (60pt,6pt) ;
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (70pt,3.5) -- (70pt,6pt) ;
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (80pt,3.5) -- (80pt,6pt) ;
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (90pt,3.5) -- (90pt,6pt) ;
\draw [black, dot diameter=4pt, dot spacing=10pt, dots] (100pt,3.5) -- (100pt,6pt) ;
\end{tikzpicture}
\caption{Two dimensional (2D) n-space of radius n and thickness dn. The density of states at an energy $\epsilon$ is the number of n-states per unit area, $A=L^2$}
\end{figure}
\end{document}
答案1
TikZ 解决方案
\documentclass[paper=a4, fontsize=12pt]{scrartcl}
\usepackage{amsmath,amsfonts,amsthm}
\usepackage{graphicx}
\usepackage{float}
\setlength\parindent{0pt}
\usepackage{tikz}
\usetikzlibrary{arrows,%
plotmarks}
\begin{document}
\begin{figure}[H]
\makeatletter
\tikzset{
dot diameter/.store in=\dot@diameter,
dot diameter=3pt,
dot spacing/.store in=\dot@spacing,
dot spacing=10pt,
dots/.style={
line width=\dot@diameter,
line cap=round,
dash pattern=on 0pt off \dot@spacing
}
}
\makeatother
\centering
\label{fig:circle}
\begin{tikzpicture}
[
mystyle/.style={black, dot diameter=4pt, dot spacing=10pt, dots}
]
% Axis
\draw[thick,->,black] (-4,0)--(4,0) node[below] {$n_x$};
\draw[thick,->,black] (0,-4)--(0,4) node[left] {$n_y$};
\draw[black,thick] (0,0) circle (3.5cm);
%dots
\draw [mystyle] (0pt,0) -- (110pt,0) ;
\foreach \x in {0,10,...,100}{\draw [mystyle] (\x pt,3.5) -- (\x pt,6pt); }
\end{tikzpicture}
\caption{Two dimensional (2D) n-space of radius n and thickness dn. The density of states at an energy $\epsilon$ is the number of n-states per unit area, $A=L^2$}
\end{figure}
\end{document}
PSTricks 解决方案
如果您确实必须减少击键次数,我提出以下内容。
\documentclass[pstricks]{standalone}
\usepackage{pst-plot}
\begin{document}
\begin{pspicture}(-4.5,-4.5)(4.5,4.5)
\psaxes[labels=none,ticks=none]{->}(0,0)(-4,-4)(4,4)[$n_x$,-90][$n_y$,180]
\pscircle{3.5}
\multips(0,0.35){11}{\multips(0.35,0){11}{\psdots(0,0)}}
\end{pspicture}
\end{document}
TikZ 解决方案(与 PSTricks 解决方案结果相同)
\documentclass[tikz,margin=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[thick]
\draw[-stealth] (-4,0)--(4,0) node[below] {$n_x$};
\draw[-stealth] (0,-4)--(0,4) node[left] {$n_y$};
\draw (0,0) circle (3.5);
\foreach \x in {0,...,10}{
\foreach \y in {0,...,10}{
\fill (\x*.35,\y*.35) circle(.06);
}
}
\end{tikzpicture}
\end{document}
最新编辑
下面的解释是为了回答提问者在评论中提出的附加问题。
使用standalone
文档类来生成紧密的图表。
% the name of this file is circle-grid.tex
\documentclass[tikz,margin=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[thick]
\draw[-stealth] (-4,0)--(4,0) node[below] {$n_x$};
\draw[-stealth] (0,-4)--(0,4) node[left] {$n_y$};
\draw (0,0) circle (3.5);
\foreach \x in {0,...,10}{
\foreach \y in {0,...,10}{
\fill (\x*.35,\y*.35) circle(.06);
}
}
\end{tikzpicture}
\end{document}
使用 编译图表 ( circle-grid.tex
)pdflatex.exe
可得到circle-grid.pdf
。
从您的主 TeX 文档(main.tex
例如,您可以将其命名为),您可以circle-grid.pdf
使用它来导入\includegraphics[]{circle-grid}
(此命令由包提供graphicx
)。
例如,主 TeX 文档如下所示。
% This filename is Main.tex
\documentclass{article}
\usepackage{graphicx}
\begin{document}
Some texts go here \ldots
\begin{figure}
\centering
\includegraphics[]{circle-grid}
\caption{Circle and grid}
\label{fig:circle-grid}
\end{figure}
Other texts go here!
\end{document}
最后编译main.tex
得到pdflatex.exe
。main.pdf
答案2
鉴于你的输出,我实际上会使用pgfplots
基于tikz
并有许多有用的environments
和commands
简化这样的图形
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{mydotstyle/.style={color=black,only marks,mark size=1pt}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,
axis equal,
xmin=-1.1,xmax=1.1,
ymin=-1.1,ymax=1.1,
xtick={-2},
ytick={-2},
xlabel=$n_x$,
ylabel=$n_y$,
xlabel style={anchor=north},
ylabel style={anchor=east},
]
\addplot[black, samples=100]({cos(deg(x))},{sin(deg(x))});
\pgfplotsinvokeforeach{0,0.1,...,1.1}
{
\addplot[mydotstyle,samples at={0,0.1,...,1.1}]{#1};
}
\end{axis}
\end{tikzpicture}
\end{document}
如果你有很多这种类型的图片,你可能需要使用类似
\pgfplotsset{mydotstyle/.style={color=black,only marks,mark size=1pt},
every axis/.append style={
axis lines=middle,
axis equal,
xmin=-1.1,xmax=1.1,
ymin=-1.1,ymax=1.1,
xtick={-2},
ytick={-2},
xlabel=$n_x$,
ylabel=$n_y$,
xlabel style={anchor=north},
ylabel style={anchor=east},
}}
在这种情况下你可以使用
\begin{tikzpicture}
\begin{axis}
\addplot...
\end{axis}
\end{tikzpicture}
答案3
\documentclass[paper=a4, fontsize=12pt]{scrartcl}
\usepackage{amsmath,amsfonts,amsthm}
\usepackage{graphicx}
\usepackage{float}
\setlength\parindent{0pt}
\usepackage{tikz}
\usetikzlibrary{arrows,%
plotmarks}
\begin{document}
\begin{figure}[H]
\centering
\label{fig:circle}
\begin{tikzpicture}
% Axis
\pgfmathsetmacro{\D}{3.5}
\draw[thick,->,black] (-4,0)--(4,0) node[below] {$n_x$};
\draw[thick,->,black] (0,-4)--(0,4) node[left] {$n_y$};
\draw[black,thick] (0,0) circle (\D);
\pgfmathsetmacro{\dx}{\D/10}
\pgfmathsetmacro{\lastx}{\D+0.0001}
\foreach \x in {0,\dx,...,\lastx}{
\foreach \y in {0,\dx,...,\lastx}{
\fill[black] (\x,\y) circle(.06);
}
}
\end{tikzpicture}
\caption{Two dimensional (2D) n-space of radius n and thickness dn.
The density of states at an energy $\epsilon$ is the number of n-states per unit area, $A=L^2$}
\end{figure}
\end{document}