Firstly, the extra space mostly comes from the extra \\
used in tabular
and the large value set to \defaultaddspace
, which is used as \\[\defaultaddspace]
in tabular
.
Secondly, the \chemfiginput
already creates a tikzpicture
environment, and it's not recommended (and generally not supported) to nest tikzpicture
. You can pass desired tikzpicture
environment options to chemfig style={...}
.
Following is an example in which
- extra
\\
is removed and \defaultaddspace
is set to appropreate value, and
- nested
tikzpicture
is removed (in definition of \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}
Follow up:
Align every \chemfiginput
at its center is not good enough. By default chemfig
uses the base of first atom as the base of the entire molecule. Since OP's molecule code is the output of some program, which might be difficult to control which atom is the first, one can use @{<node name>}
before any atom and use chemfig style={baseline=(<node name>.base)}
to use the base of marked atom as that of the entire molecule.
Furthermore, in the following enhanced example a new tikz
option enlarge bounding box by
is provided in order to prohibit the \\[\defaultaddspace]
and give more consistent vertical space.
This is nicer both in vertical alignment and in vertical space, isn't it?
\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}
A mixing of finer surrounding spaces and not touching the contents of chemfig
objects
Main changes:
- new tikz
option enlarge bounding box by
- \defaultaddspace
is used to specify the extra vertical spaces around every \callchemfigsymbol
- use \\
instead of \\[\defaultaddspace]
inside 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}
PS
Since LaTeX2e version 2019-10-01, the utility of filecontents
package has been merged in the LaTeX2e format, so there is no need to load filecontents
package.