foreach 中的字母

foreach 中的字母

下面的代码

\documentclass[11pt]{article}
 \usepackage{amsmath}
 \usepackage{amsfonts}
 \usepackage{amssymb}
 \usepackage{amsthm}
 \usepackage[english, greek]{babel}
 \usepackage[utf8]{inputenc}
 \usepackage{kmath, kerkis}
 \usepackage{pgf, tikz}
 \usetikzlibrary{arrows}
 \usepackage{caption}
 \usepackage{float}

  %custom commands 

  \newcommand{\A}{{\rm A}}
  \newcommand{\B}{{\rm B}}
 \begin{document}
 \begin{figure}[H]
 \centering
\begin{tikzpicture}[scale=0.7]
\draw [->, thick] (-2, 0) -- (6, 0);
\draw (6, 0) node[below]{$x$};
\draw [->, thick] (-1, -1) -- (2, 5);
\draw (2, 5) node[above]{$y$};
\draw (-0.7, 0) node[above]{${\rm O}$};
\draw [shift={(-0.5,0)},color=black,fill=black] (0,0) -- (0:0.6) arc (0:63.43:0.6) -- cycle;
\draw (0.3, 0.3) node[above]{$\theta$};
\foreach \i in {2, 3, 4}
{\draw [fill=black] (\i, \i) circle(2pt);   }  
\draw (2, 2) node[above]{$\A$};
\draw (3, 3) node[above]{$\B$};
\draw (4, 4) node[above]{$\Gamma$};
\draw (2, 2) -- (4, 4); 
\end{tikzpicture}
\captionsetup{labelformat=empty}
\caption{Σχήμα 16 } \label{fig16}
\end{figure}
\end{document}

在此处输入图片描述

从我用来绘制三个圆圈的代码中可以看出\foreach。我怎样才能用同样的方式(使用\foreach)放置图中所示的节点 A、B、Γ?在这个特定情况下,只有三个点,所以我手动完成。如果有 10 个或更多怎么办?

答案1

您可以使用计数器,然后使用将其转换为字母\Alph

字母

\documentclass[11pt]{article}
 \usepackage{amsmath}
 \usepackage{amsfonts}
 \usepackage{amssymb}
 \usepackage{amsthm}
 \usepackage[english, greek]{babel}
 \usepackage[utf8]{inputenc}
 \usepackage{kmath, kerkis}
 \usepackage{pgf, tikz}
 \usetikzlibrary{arrows}
 \usepackage{caption}
 \usepackage{float}

 \newcounter{ccount}

 \begin{document}
 \begin{figure}[H]
 \centering
\begin{tikzpicture}[scale=0.7]
\draw [->, thick] (-2, 0) -- (6, 0);
\draw (6, 0) node[below]{$x$};
\draw [->, thick] (-1, -1) -- (2, 5);
\draw (2, 5) node[above]{$y$};
\draw (-0.7, 0) node[above]{$\mathrm{O}$};
\draw [shift={(-0.5,0)},color=black,fill=black] (0,0) -- (0:0.6) arc (0:63.43:0.6) -- cycle;
\draw (0.3, 0.3) node[above]{$\theta$};
\foreach \i in {2, 3, 4}
{
      \setcounter{ccount}{\i}
      \addtocounter{ccount}{-1}
    \draw [fill=black] (\i, \i) circle(2pt) node[above] {\Alph{ccount}};
}
\draw (2, 2) -- (4, 4); 
\end{tikzpicture}
\captionsetup{labelformat=empty}
\caption{Σχήμα 16 } \label{fig16}
\end{figure}
\end{document}

答案2

您可以使用\foreach带有两个变量的(请参阅p.903,pgfmanual v3.0.1a):

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage[english, greek]{babel}
\usepackage[utf8]{inputenc}
\usepackage{kmath, kerkis}
\usepackage{pgf, tikz}
\usetikzlibrary{arrows}
\usepackage{caption}
\usepackage{float}

\newcommand{\A}{\mathrm{A}}% \rm deprecated for 25 years
\newcommand{\B}{\mathrm{B}}

\begin{document}
\begin{figure}[H]
  \centering
  \begin{tikzpicture}[scale=0.7]
    \draw [->, thick] (-2, 0) -- (6, 0);
    \draw (6, 0) node[below]{$x$};
    \draw [->, thick] (-1, -1) -- (2, 5);
    \draw (2, 5) node[above]{$y$};
    \draw (-0.7, 0) node[above]{$\mathrm{O}$};
    \draw [shift={(-0.5,0)},color=black,fill=black] (0,0) -- (0:0.6) arc (0:63.43:0.6) -- cycle;
    \draw (0.3, 0.3) node[above]{$\theta$};
    \foreach \i/\letter in {2/\A, 3/\B, 4/\Gamma}{
      \draw [fill=black] (\i, \i) circle(2pt) node[above]{$\letter$};
    }  
    \draw (2, 2) -- (4, 4); 
  \end{tikzpicture}
  \captionsetup{labelformat=empty}
  \caption{Σχήμα 16} \label{fig16}
\end{figure}
\end{document}

相关内容