我可能忽略了一些基本的东西,希望得到一些提示。请参阅插图 mwe。
对于拥有一个 chemfig 符号库,我认为能够通过命令指向特定符号(存储在外部 .tex 文件中)很有价值\input{}
,明显的构造是:\chemfig[][]{\input{<chemfig symbol library path>/{<symbol name>}}}
。
\documentclass[preview,border=7pt,active,tightpage]{standalone}
\usepackage{booktabs}
\usepackage{chemfig}
\usepackage{filecontents}
\usepackage[scaled]{helvet}
%\begin{filecontents}{methane.tex}
%H% 2
% -[:210]% 1
% (
% -[:210]H% 3
% )
% (
% -[:300]H% 5
% )
% -[:120]H% 4
%\end{filecontents}
% representation without comments
\begin{filecontents}{methane.tex}
H
-[:210]
(
-[:210]H
)
(
-[:300]H
)
-[:120]H
\end{filecontents}
\DeclareRobustCommand{\robustinputcommand}{\input{methane.tex}}
% sans serif font
\renewcommand\familydefault{\sfdefault}
% define formulae
\def\methane{\mathrm{CH_{4}}}
\begin{document}
\begin{center}
\begin{tabular}[]{lp{1.5cm}}
\toprule
\textbf{Formula} & \textbf{Structure}\\
\midrule%
%$\methane$ & \begin{minipage}[]{1cm} \chemfig{\input{methane.tex}} \end{minipage} \\ [2mm]% gives error
%$\methane$ & \begin{minipage}[]{1cm} \chemfig{\protect\input{methane.tex}} \end{minipage} \\ [2mm]% gives error
%$\methane$ & \begin{minipage}[]{1cm} \chemfig{\robustinputcommand} \end{minipage} \\ [2mm]% gives error
\bottomrule
\end{tabular}
\end{center}
\end{document}
这是我持续观察到的错误:
! Undefined control sequence.
<everyeof> \_nil
我尝试了一些方法,特别是下面描述的方法这里。这些在 mwe 中有评论。
请注意,构造:(\input{<full file path>}
文档级调用)和\chemfig[][]{<content defining symbol>}
(外部文档的通用结构)工作正常,但这并不允许立即应用格式,例如[scale=0.5]
作为文档级别的可选参数\chemfig
- 能够做到这一点显然很有价值。
答案1
这是一个\chemfiginput
也接受可选参数的命令,例如\chemfig
。
% representation without comments
\begin{filecontents}{methane.tex}
H
-[:210]
(
-[:210]H
)
(
-[:300]H
)
-[:120]H
\end{filecontents}
\documentclass{article}
\usepackage{booktabs}
\usepackage{chemfig}
\usepackage[scaled]{helvet}
\usepackage{catchfile}
% sans serif font
\renewcommand\familydefault{\sfdefault}
% define formulae
\newcommand\methane{\ensuremath{\mathrm{CH_{4}}}}
\newcommand{\chemfiginput}[2][]{%
\CatchFileDef{\chemfiginputtemp}{#2}{\csname CF_sanitizecatcode\endcsname}%
\expandafter\chemfigdo\expandafter{\chemfiginputtemp}{#1}%
}
\newcommand{\chemfigdo}[2]{\chemfig[#2]{#1}}
\begin{document}
\begin{tabular}[]{ll}
\toprule
\textbf{Formula} & \textbf{Structure}\\
\midrule
\methane & \chemfiginput{methane} \\
\methane & \chemfiginput[chemfig style={color=red!40!black, line width=1.5pt}]{methane} \\
\methane & \chemfig{H-[:210](-[:210]H)(-[:300]H)-[:120]H} \\
\bottomrule
\end{tabular}
\end{document}
答案2
\chemfig{
将和包含}
进去methane.tex
而不是用它包围\input
命令似乎有效:
\documentclass[preview,border=7pt,active,tightpage]{standalone}
\usepackage{booktabs}
\usepackage{chemfig}
\usepackage{filecontents}
\usepackage[scaled]{helvet}
\usepackage{adjustbox}
\begin{filecontents}{methane.tex}
\chemfig{
H
-[:210]
(
-[:210]H
)
(
-[:300]H
)
-[:120]H}
\end{filecontents}
% sans serif font
\renewcommand\familydefault{\sfdefault}
% define formulae
\def\methane{$\mathrm{CH_{4}}$}
\begin{document}
\begin{center}
\begin{tabular}[]{ll}
\toprule
\textbf{Formula} & \textbf{Structure}\\
\midrule
\methane & \adjustbox{valign=m}{\input{methane.tex}}\\ [2mm]
\bottomrule
\end{tabular}
\end{center}
\end{document}
答案3
\chemfig
删除外部文件调用的棘手之处.tex
在于,它会在读取其强制参数之前更改类别代码。为了解决这个问题,我使用包\CatchFileDef
中的catchfile
来保存 的内容,methane.tex
并使用与 相同的类别代码设置\chemfig
。
下面的代码是通用的,可以毫不费力地用于其他分子。Ti钾用于分子结构打印的 Z 选项可以在中心位置 ( \myprintmol
) 指定,并且没有冗余tabular
:只需用逗号分隔分子名称:
\documentclass{article}
\usepackage{booktabs}
\usepackage{array} % for \newcolumntype
\usepackage{collcell} % for \collectcell
\usepackage{chemfig}
\usepackage{filecontents}
\usepackage{catchfile} % for \CatchFileDef
\usepackage{adjustbox} % for \adjustbox
\usepackage{etoolbox} % for \forcsvlist
% cf. <https://tex.stackexchange.com/a/98011/73317> (Ulrike Fischer)
\DeclareMathAlphabet{\mathup}{T1}{\familydefault}{m}{n}
\newcommand*{\mydeclaremolstruct}[1]{%
\expandafter\CatchFileDef\expandafter{\csname #1struct\endcsname}{#1.tex}%
{\csname CF_sanitizecatcode\endcsname}%
}
\newcommand*{\myprintmol}[1]{%
\begingroup
% Here, you can customize how molecule structure is printed (TikZ options)
\setchemfig{chemfig style={color=red!40!black, line width=1.5pt}}%
%
\adjustbox{valign=M}{% To align with vertical center of the whole molecule
\expandafter\chemfig\expandafter{#1}% Process tokenized contents from
}% % external per-molecule .tex files
\endgroup
}
\begin{filecontents}{methane.tex}
H
-[:210]
(
-[:210]H
)
(
-[:300]H
)
-[:120]H
\end{filecontents}
\mydeclaremolstruct{methane} % base name of the corresponding .tex file
\newcommand*{\methane}{\mathup{CH}_{4}}
% Special column type for representing the molecule structure
\newcolumntype{M}{>{\collectcell\myprintmol}c<{\endcollectcell}}
\newcommand*{\generatetableline}[1]{%
$\csname #1\endcsname$ & \csname #1struct\endcsname \\
}
\begin{document}
\begin{tabular}{lM}
\toprule
\textbf{Formula} & \multicolumn{1}{c}{\textbf{Structure}}\\
\midrule
\forcsvlist{\generatetableline}{methane}%
%\forcsvlist{\generatetableline}{methane, ethane, propane}%
\bottomrule
\end{tabular}
\end{document}
以下是稍微简化的表格构造,但自动化程度也略低:
\documentclass{article}
\usepackage{booktabs}
\usepackage{array} % for \newcolumntype
\usepackage{collcell} % for \collectcell
\usepackage{chemfig}
\usepackage{filecontents}
\usepackage{catchfile} % for \CatchFileDef
\usepackage{adjustbox} % for \adjustbox
% cf. <https://tex.stackexchange.com/a/98011/73317> (Ulrike Fischer)
\DeclareMathAlphabet{\mathup}{T1}{\familydefault}{m}{n}
\newcommand*{\mydeclaremolstruct}[1]{%
\expandafter\CatchFileDef\expandafter{\csname #1struct\endcsname}{#1.tex}%
{\csname CF_sanitizecatcode\endcsname}%
}
\newcommand*{\myprintmol}[1]{%
\begingroup
% Here, you can customize how molecule structure is printed (TikZ options)
\setchemfig{chemfig style={color=red!40!black, line width=1.5pt}}%
%
\adjustbox{valign=M}{% To align with vertical center of the whole molecule
\expandafter\chemfig\expandafter{#1}% Process tokenized contents from
}% % external per-molecule .tex files
\endgroup
}
\begin{filecontents}{methane.tex}
H
-[:210]
(
-[:210]H
)
(
-[:300]H
)
-[:120]H
\end{filecontents}
\mydeclaremolstruct{methane} % base name of the corresponding .tex file
\newcommand*{\methane}{\mathup{CH}_{4}}
% Special column type for representing the molecule structure
\newcolumntype{M}{>{\collectcell\myprintmol}c<{\endcollectcell}}
\begin{document}
\begin{tabular}{>{$}l<{$}M}
\toprule
\textbf{Formula} & \multicolumn{1}{c}{\textbf{Structure}}\\
\midrule
\methane & \methanestruct \\
\bottomrule
\end{tabular}
\end{document}
(与上一张截图的输出相同)。