答案1
\atom{#1}{#2}{#3}
我为两个版本(\chemfig{A*6(-A-A-A-A-A-)}
和)创建了命令\chemfig{**6((-A)-(-A)-(-A)-(-A)-(-A)-(-A)-)}
。代码中充满了 for 循环。版本(* 和 **)用 处理ifcase
。
输入参数:
- #1:使用 1 或 2 -> 1 = 环;2 = 环/内弧
- #2:元素数量 -> 例如 6
- #3:化学元素符号 ->
结果\atom{1}{6}{A}
:\atom{2}{6}{B}
梅威瑟:
\documentclass{article}
\usepackage{chemfig}
\newcommand{\atom}[3]
{
%#1: * or ** => 1 or 2
%#2: number of elements => number
%#3: element => letter
\ifcase#1
\or%\if#1=1
\def\CZ{#3}
\def\N{#2}
\def\myarray{-}
\pgfmathparse{\N-1}
\def\lastIndex{\pgfmathresult}
\foreach \i in {1,2,...,\lastIndex}
{
\edef\myarray{\myarray\CZ-}
\afterforeachdef\myarray
}
\edef\myarray{\CZ*\N(\myarray)}
\expandafter\chemfig\expandafter{\myarray}%https://tex.stackexchange.com/q/399618/124842
\or%\if#1=2 %Version 2 with **
\def\CZ{#3}
\def\N{#2}
\def\myarray{}
\pgfmathparse{\N}
\def\lastIndex{\pgfmathresult}
\foreach \i in {1,2,...,\lastIndex}
{
\edef\myarray{\myarray(-\CZ)-}
\afterforeachdef\myarray
}
\edef\myarray{**\N(\myarray)}
\expandafter\chemfig\expandafter{\myarray}
\else
Wrong input number! Use 1 or 2 (argument \#1).
\fi
}
% ------------------------------------------------------------
%%%%% @Paul Gaborit:https://tex.stackexchange.com/a/69508/124842
\usepackage{etextools}
\makeatletter
% define a macro after current iteration
\newcommand\afteriterationdef[1]{\aftergroup@def#1}
% define a macro after foreach (and after iteration)
\newcommand\afterforeachdef[1]{\afteriterationdef{#1}\AfterGroup{\aftergroup@def#1}}
\makeatother
% ------------------------------------------------------------
\begin{document}
\atom{1}{6}{A}
\atom{2}{6}{B}
\end{document}
參考文獻:
@PaulGaborit:如何在 foreach 循环中定义宏并在迭代之间和循环后产生效果而不使用全局?
@egreg:是否可以使用宏作为 \chemfig 的输入参数?