下面的实现寻求将\chemfig
对象在表格环境的中心对齐列内水平居中。
得到意想不到的结果。请参阅周围的评论\begin{tabular}
。这表明chemfig
对象携带了额外的空间?
\documentclass[border=6pt,active]{standalone}%
\usepackage{booktabs}%
\usepackage{miscchemsym}%
\usepackage{chemfig}%
\usepackage{filecontents}%
\usepackage{tikz}%
\usepackage{catchfile}%
% chemfigdo
\newcommand{\chemfigdo}[2]{\chemfig[#2]{#1}}%
% chemfiginput
\newcommand{\chemfiginput}[2][]{%
\CatchFileDef{\chemfiginputtemp}{#2}{\csname CF_sanitizecatcode\endcsname}%
\expandafter\chemfigdo\expandafter{\chemfiginputtemp}{#1}%
}%
% create new command for tikzpicture call
\newcommand\callchemfigsymbol[1]{%
\begin{tikzpicture}[scale=1,%
xscale=-1,%
yscale=-1,%
inner sep=0pt,%
outer sep=0pt,%
baseline=(current bounding box.center)]%
\chemfiginput[%
atom style={scale=\chemfigscale},%
atom sep=\chemfigatomsep,%
chemfig style={color=\chemfigcolor,line width=\chemfiglinewidth}%
]{#1}%
\end{tikzpicture}%
}%
% define the atom sep
\newlength{\chemfigatomsep}%
\setlength{\chemfigatomsep}{2.75em}%
% define the line width
\newlength{\chemfiglinewidth}%
\setlength{\chemfiglinewidth}{0.75pt}%
% define the scale
\def\chemfigscale{0.8}
% define the color
\def\chemfigcolor{red!40!black}
% define separation length
\setlength{\defaultaddspace}{13mm}
\begin{document}
\begin{filecontents}{methane_pseudo_3D_stereo.tex}
H% 2
-[:270]C% 1
(
-[:200]H% 4
)
(
<[:310]H% 5
)
<:[:350]H% 3
\end{filecontents}
\begin{filecontents}{sulfurhexafluoride_pseudo_3D_stereo.tex}
F% 1
-[:90]S% 2
(
<[:337.5]F% 3
)
(
<[:202.5]F% 4
)
(
<:[:22.5]F% 5
)
(
-[:90]F% 7
)
<:[:157.5]F% 6
\end{filecontents}%
\begin{filecontents}{trifluoromethylsulfurpentafluoride_pseudo_3D_stereo.tex}
F% 2
-[:300]C% 1
(
-[:240]F% 3
)
(
-[:180]F% 4
)
-S% 5
(
<[:240]F% 6
)
(
<[:120]F% 7
)
(
<:[:300]F% 8
)
(
-F% 10
)
<:[:60]F% 9
\end{filecontents}%
\begin{tabular}[]{cc}
%\begin{tabular}[]{cp{4cm}}
\toprule
\textbf{Compound name} & \textbf{Structure} \\%
\midrule%
\\%
Methane & \callchemfigsymbol{methane_pseudo_3D_stereo.tex} \\%
[\defaultaddspace]%
%
Sulfurhexafluoride & \callchemfigsymbol{sulfurhexafluoride_pseudo_3D_stereo.tex} \\%
[\defaultaddspace]%
%
Trifluoromethyl-sulfurpentafluoride & \callchemfigsymbol{trifluoromethylsulfurpentafluoride_pseudo_3D_stereo.tex} \\%
\\%
\bottomrule
\end{tabular}
\end{document}
答案1
首先,额外的空间主要来自于\\
中使用的额外空间tabular
和 设置的较大值\defaultaddspace
,其用法与\\[\defaultaddspace]
中一样tabular
。
其次,\chemfiginput
已经创建了一个tikzpicture
环境,不建议(通常也不支持)嵌套tikzpicture
。您可以将所需的tikzpicture
环境选项传递给chemfig style={...}
。
下面是一个例子
- 额外的
\\
被删除,并\defaultaddspace
设置为适当的值,并且 - 嵌套
tikzpicture
被删除(在 的定义中\callchemfigsymbol
)
\documentclass[border=6pt,active]{standalone}%
\usepackage{booktabs}%
%\usepackage{miscchemsym}%
\usepackage{chemfig}%
\usepackage{filecontents}%
\usepackage{tikz}%
\usepackage{catchfile}%
% chemfigdo
\newcommand{\chemfigdo}[2]{\chemfig[#2]{#1}}%
% chemfiginput
\newcommand{\chemfiginput}[2][]{%
\CatchFileDef{\chemfiginputtemp}{#2}{\csname CF_sanitizecatcode\endcsname}%
\expandafter\chemfigdo\expandafter{\chemfiginputtemp}{#1}%
}%
% create new command for tikzpicture call
\newcommand\callchemfigsymbol[1]{%
\chemfiginput[
atom style={scale=\chemfigscale},
atom sep=\chemfigatomsep,
chemfig style={
baseline=(current bounding box.center),
color=\chemfigcolor,
line width=\chemfiglinewidth
}
]{#1}%
}
% define the atom sep
\newlength{\chemfigatomsep}%
\setlength{\chemfigatomsep}{2.75em}%
% define the line width
\newlength{\chemfiglinewidth}%
\setlength{\chemfiglinewidth}{0.75pt}%
% define the scale
\def\chemfigscale{0.8}
% define the color
\def\chemfigcolor{red!40!black}
% define separation length
\setlength{\defaultaddspace}{25pt}
\begin{document}
\begin{filecontents}{methane_pseudo_3D_stereo.tex}
H% 2
-[:270]C% 1
(
-[:200]H% 4
)
(
<[:310]H% 5
)
<:[:350]H% 3
\end{filecontents}
\begin{filecontents}{sulfurhexafluoride_pseudo_3D_stereo.tex}
F% 1
-[:90]S% 2
(
<[:337.5]F% 3
)
(
<[:202.5]F% 4
)
(
<:[:22.5]F% 5
)
(
-[:90]F% 7
)
<:[:157.5]F% 6
\end{filecontents}%
\begin{filecontents}{trifluoromethylsulfurpentafluoride_pseudo_3D_stereo.tex}
F% 2
-[:300]C% 1
(
-[:240]F% 3
)
(
-[:180]F% 4
)
-S% 5
(
<[:240]F% 6
)
(
<[:120]F% 7
)
(
<:[:300]F% 8
)
(
-F% 10
)
<:[:60]F% 9
\end{filecontents}%
\begin{tabular}{cc}
\toprule
\textbf{Compound name} & \textbf{Structure} \\
\midrule
Methane & \callchemfigsymbol{methane_pseudo_3D_stereo.tex} \\[\defaultaddspace]
Sulfurhexafluoride & \callchemfigsymbol{sulfurhexafluoride_pseudo_3D_stereo.tex} \\[\defaultaddspace]
Trifluoromethyl-sulfurpentafluoride & \callchemfigsymbol{trifluoromethylsulfurpentafluoride_pseudo_3D_stereo.tex} \\[\defaultaddspace]
\bottomrule
\end{tabular}
\end{document}
跟进:
将每个都对齐\chemfiginput
到其中心还不够好。默认情况下,chemfig
使用第一个原子的碱基作为整个分子的碱基。由于 OP 的分子代码是某个程序的输出,可能很难控制哪个原子是第一个,因此可以@{<node name>}
在任何原子之前使用并使用chemfig style={baseline=(<node name>.base)}
标记原子的碱基作为整个分子的碱基。
此外,在下面的增强示例中,提供了一个新tikz
选项enlarge bounding box by
,以禁止\\[\defaultaddspace]
并提供更一致的垂直空间。
\documentclass[border=6pt,active]{standalone}%
\usepackage{booktabs}%
%\usepackage{miscchemsym}%
\usepackage{chemfig}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{catchfile}
% chemfigdo
\newcommand{\chemfigdo}[2]{\chemfig[#2]{#1}}
% chemfiginput
\newcommand{\chemfiginput}[2][]{%
\CatchFileDef{\chemfiginputtemp}{#2}{\csname CF_sanitizecatcode\endcsname}%
\expandafter\chemfigdo\expandafter{\chemfiginputtemp}{#1}%
}
\tikzset{
% #1 = left, #2 = lower, #3 = right, #4 = upper
enlarge bounding box by/.style n args={4}{
execute at end picture={
\path ([shift={(-#1, -#2)}]current bounding box.south west) --
([shift={(#3, #4)}]current bounding box.north east);
}
}
}
% create new command for tikzpicture call
\newcommand\callchemfigsymbol[2][current bounding box.center]{%
\chemfiginput[
atom style={scale=\chemfigscale},
atom sep=\chemfigatomsep,
chemfig style={
enlarge bounding box by={0pt}{3pt}{0pt}{3pt},
baseline=(#1),
color=\chemfigcolor,
line width=\chemfiglinewidth
}
]{#2}%
}
% define the atom sep
\newlength{\chemfigatomsep}%
\setlength{\chemfigatomsep}{2.75em}%
% define the line width
\newlength{\chemfiglinewidth}%
\setlength{\chemfiglinewidth}{0.75pt}%
% define the scale
\def\chemfigscale{0.8}
% define the color
\def\chemfigcolor{red!40!black}
% define separation length
%\setlength{\defaultaddspace}{20pt}
\begin{document}
\begin{filecontents}[overwrite]{methane_pseudo_3D_stereo.tex}
H% 2
-[:270]@{base}C% 1
(
-[:200]H% 4
)
(
<[:310]H% 5
)
<:[:350]H% 3
\end{filecontents}
\begin{filecontents}[overwrite]{sulfurhexafluoride_pseudo_3D_stereo.tex}
F% 1
-[:90]@{base}S% 2
(
<[:337.5]F% 3
)
(
<[:202.5]F% 4
)
(
<:[:22.5]F% 5
)
(
-[:90]F% 7
)
<:[:157.5]F% 6
\end{filecontents}%
\begin{filecontents}[overwrite]{trifluoromethylsulfurpentafluoride_pseudo_3D_stereo.tex}
F% 2
-[:300]C% 1
(
-[:240]F% 3
)
(
-[:180]@{base}F% 4
)
-S% 5
(
<[:240]F% 6
)
(
<[:120]F% 7
)
(
<:[:300]F% 8
)
(
-F% 10
)
<:[:60]F% 9
\end{filecontents}%
\begin{tabular}{cc}
\toprule
\textbf{Compound name} & \textbf{Structure} \\
\midrule
Methane & \callchemfigsymbol[base.base]{methane_pseudo_3D_stereo.tex} \\
Sulfurhexafluoride & \callchemfigsymbol[base.base]{sulfurhexafluoride_pseudo_3D_stereo.tex} \\
Trifluoromethyl-sulfurpentafluoride & \callchemfigsymbol[base.base]{trifluoromethylsulfurpentafluoride_pseudo_3D_stereo.tex} \\
\bottomrule
\end{tabular}
\end{document}
混合更精细的周围空间,不接触chemfig
物体的内容
主要变化: - 新tikz
选项enlarge bounding box by
-\defaultaddspace
用于指定每个周围的额外垂直空间\callchemfigsymbol
- 使用\\
而不是\\[\defaultaddspace]
内部tabular
\documentclass[border=6pt,active]{standalone}%
\usepackage{booktabs}%
%\usepackage{miscchemsym}%
\usepackage{chemfig}%
\usepackage{filecontents}%
\usepackage{tikz}%
\usepackage{catchfile}%
\tikzset{
% #1 = left, #2 = lower, #3 = right, #4 = upper
enlarge bounding box by/.style n args={4}{
execute at end picture={
\path ([shift={(-#1, -#2)}]current bounding box.south west) --
([shift={(#3, #4)}]current bounding box.north east);
}
}
}
% chemfigdo
\newcommand{\chemfigdo}[2]{\chemfig[#2]{#1}}%
% chemfiginput
\newcommand{\chemfiginput}[2][]{%
\CatchFileDef{\chemfiginputtemp}{#2}{\csname CF_sanitizecatcode\endcsname}%
\expandafter\chemfigdo\expandafter{\chemfiginputtemp}{#1}%
}%
% create new command for tikzpicture call
\newcommand\callchemfigsymbol[1]{%
\chemfiginput[
atom style={scale=\chemfigscale},
atom sep=\chemfigatomsep,
chemfig style={
enlarge bounding box by={0pt}{\defaultaddspace}{0pt}{\defaultaddspace},
baseline=(current bounding box.center),
color=\chemfigcolor,
line width=\chemfiglinewidth
}
]{#1}%
}
% define the atom sep
\newlength{\chemfigatomsep}%
\setlength{\chemfigatomsep}{2.75em}%
% define the line width
\newlength{\chemfiglinewidth}%
\setlength{\chemfiglinewidth}{0.75pt}%
% define the scale
\def\chemfigscale{0.8}
% define the color
\def\chemfigcolor{red!40!black}
% define separation length
\setlength{\defaultaddspace}{3pt}
\begin{document}
%% To save space, three filecontents environments are ignored
\begin{tabular}{cc}
\toprule
\textbf{Compound name} & \textbf{Structure} \\
\midrule
Methane & \callchemfigsymbol{methane_pseudo_3D_stereo.tex} \\
Sulfurhexafluoride & \callchemfigsymbol{sulfurhexafluoride_pseudo_3D_stereo.tex} \\
Trifluoromethyl-sulfurpentafluoride & \callchemfigsymbol{trifluoromethylsulfurpentafluoride_pseudo_3D_stereo.tex} \\
\bottomrule
\end{tabular}
\end{document}
附言
自 LaTeX2e 版本 2019-10-01 起,包的实用程序filecontents
已合并到 LaTeX2e 格式中,因此无需加载filecontents
包。