2

Implementation below seeks to center \chemfig objects horizontally within a center-aligned column of a tabular environment.

Getting unexpected results. Please see commenting around the \begin{tabular}. Suggests that chemfig objects are carrying around extra space?

\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 Answer 1

3

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}

enter image description here

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? enter image description here

\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}

enter image description here

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.

5
  • Legend. When I try to externalize the setup commands, I'm getting: ! Package pgf Error: No shape named base is known
    – John Chris
    Commented May 15, 2020 at 7:38
  • The moment that I strip away the invocations of filecontents and instead point the \callchemfigsymbol{} commands to absolute file paths (e.g. C:/library/chemfig/<symbol file name>.tex) I receive the above error.
    – John Chris
    Commented May 15, 2020 at 8:50
  • 1
    @JohnChris There are three @{base} added to contents of filecontents env. Commented May 15, 2020 at 20:07
  • Thank you. A solution where the chemfig object must be modified is not ideal. Consider a large library of chemfig symbols which are generated from .mol or .sdf files automatically. Also consider that the table described in this post may be long and will be populated with a variety of different compounds / changed and re-generated frequently. The requirement for a user to go into the chemfig object and manually select the base is too time consuming / adds complexity to the workflow. Can we think of alternatives?
    – John Chris
    Commented May 21, 2020 at 10:28
  • @JohnChris I have appended a new solution variant to the end of my answer, in which vertical spacing is finer and chemfig objects is not touched. Commented May 21, 2020 at 11:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .