# Adjust chemical names to the width of the text

I have several little questions:

1. How do I get the names of the chemical compounds to adapt to the width of the text? The problem occurs with propyl 4-hydroxybenzoate.
2. Is the code okay, or could something be improved/made more elegant?
3. I don't quite understand the command \begin{tabular*}{\textwidth}{c @{\extracolsep{\fill}} cc}. Does the c have to be before the @ sign, or could I do it that way too \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} ccc}
4. Actually, I could have left out \begin{center} and \end{center}, right? Wouldn't that change anything?

I hope you can answer all of my questions.

\documentclass[english]{scrartcl}

\usepackage{babel}
\usepackage{blindtext}
\usepackage{chemfig}
\setchemfig{atom sep=2.25em}
\usepackage{booktabs}

\begin{document}

\section{Structural formula}

\begin{center}
\schemestart
\begin{tabular*}{\textwidth}{c @{\extracolsep{\fill}} cc}
\chemname{
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OMe)=-)}
}{Methyl 4-hydroxybenzoate}
&
\chemname{
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OEt)=-)}
}{Ethyl 4-hydroxybenzoate}
&
\chemname{
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OPr)=-)}
}{Propyl 4-hydroxybenzoate}
\\
\addlinespace[2ex]
\chemname{
\chemfig{*6((=O)-N(-Me)-*5(-N=-N(-Me)-=)--(=O)-N(-Me)-)}
}{Caffeine}
&
\chemname{
\chemfig{H_2N-[:30](=[:90]S)-[::-60]NH_2}
}{Thiourea}
&
\chemname{
\chemfig{H-C(-[2]H)(-[6]H)-OH}
}{Methanol}
\end{tabular*}
\schemestop
\end{center}

\section{Experimental procedure}

\blindtext

\end{document}


• My suggestion is not to use \chemname. Its alignment behavior seems quite unpredictable to me. Jul 24 at 22:30
• I am voting NOT to close this question. Although there are multiple subquestions, they are basically different aspects of the same thing. Jul 24 at 22:33
• @modnar Your knowledge of the chemistry packages is outstanding!!! :) Maybe the chemfig package's author should be made aware of the problems with \chemname... Jul 25 at 3:24
• Entirely unrelated to the alignment issue, but simply out of curiosity: What kind of document are these formulae supposed to be used in? The section headings look like this is some kind of experimental part, the image however is quite unusual for an experimental part in a chemistry paper or thesis. Jul 25 at 7:24
• @leandriis This is a template that the professor gave me and which I have to adhere to. Jul 25 at 13:09

## 3 Answers

Below is my attempt:

\documentclass[english]{scrartcl}

\usepackage{babel}
\usepackage{blindtext}
\usepackage{chemfig}
\setchemfig{atom sep=2.25em}
\usepackage{booktabs}

\begin{document}
\section{Structural formula}
\blindtext

\begin{center}
\schemestart
\begin{tabular*}{\textwidth}{@{}c@{\extracolsep{\fill}}c@{\extracolsep{\fill}}c@{}}
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OMe)=-)}
&
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OEt)=-)}
&
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OPr)=-)}\\
\\
Methyl 4-hydroxybenzoate
&
Ethyl 4-hydroxybenzoate
&
Propyl 4-hydroxybenzoate\\
\\
\chemfig{*6((=O)-N(-Me)-*5(-N=-N(-Me)-=)--(=O)-N(-Me)-)}
&
\chemfig{H_2N-[:30](=[:90]S)-[::-60]NH_2}
&
\chemfig{H-C(-[2]H)(-[6]H)-OH}\\
\\
Caffeine
&
Thiourea
&
Methanol
\end{tabular*}
\schemestop
\end{center}

\section{Experimental procedure}
\blindtext

\end{document}


I don't know why the center environment seems necessary for the correct layout.

Here, I make two changes:

1. I put the offending name into a stack, to break it across two lines

2. I add two spaces ~~ to the end of each line to push the chem figure slightly leftward, allowing the extra space for the name.

The MWE.

\documentclass[english]{scrartcl}

\usepackage{babel}
\usepackage{blindtext}
\usepackage{chemfig}
\setchemfig{atom sep=2.25em}
\usepackage{booktabs,stackengine}

\begin{document}

\section{Structural formula}

\begin{center}
\schemestart
\begin{tabular*}{\textwidth}{c @{\extracolsep{\fill}} cc}
\chemname{
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OMe)=-)}
}{Methyl 4-hydroxybenzoate}
&
\chemname{
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OEt)=-)}
}{Ethyl 4-hydroxybenzoate}
&
\chemname{
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OPr)=-)}
}{\Longunderstack{Propyl 4-hydroxybenzoate}}
~~\\
\addlinespace[2ex]
\chemname{
\chemfig{*6((=O)-N(-Me)-*5(-N=-N(-Me)-=)--(=O)-N(-Me)-)}
}{Caffeine}
&
\chemname{
\chemfig{H_2N-[:30](=[:90]S)-[::-60]NH_2}
}{Thiourea}
&
\chemname{
\chemfig{H-C(-[2]H)(-[6]H)-OH}
}{Methanol}~~
\end{tabular*}
\schemestop
\end{center}

\section{Experimental procedure}

\blindtext

\end{document}


• Is there a difference between \begin{tabular*}{\textwidth}{c @{\extracolsep{\fill}} cc} and \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} ccc} and \begin{tabular*}{\textwidth}{@{}c@{\extracolsep{\fill}}c@{\extracolsep{\fill}}c@{}}? Jul 24 at 23:03
• @Schubladenzieher The basic column specification is ccc, which comes with \tabcolsep separation between columns. That spacing between any two columns can be overridden with @{} specifications between specific columns. So, @{} means add no spacing between that column gap. @{\extracolsep{\fill}} means add the extra separation needed to meet the specified width requirement. So placing that between different columns can affect the inter-column gaps. Jul 24 at 23:14
• So: The way @modnar wrote it is the actual correct way? Jul 24 at 23:46
• @Schubladenzieher Correct is whatever gives you the look you desire. If you want equal space between all columns, then you need to add the same @{} specification to both gaps. However, since I added the ~~ buffer to the right side, the perception of "correct" might be slightly different. Jul 25 at 2:30

This version uses \tabularx instead of tabular*. The main difference is that all the columns will be the same width. The main advantage is that \linewidth is the available space, so one can use \parbox[t]{\linewidth}{\centering ...} to handle the names.

\documentclass[english]{scrartcl}

\usepackage{babel}
\usepackage{blindtext}
\usepackage{chemfig}
\setchemfig{atom sep=2.25em}
\usepackage{booktabs}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage{showframe}

\begin{document}

\section{Structural formula}

\schemestart
\begin{tabularx}{\textwidth}{CCC}
\chemname{
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OMe)=-)}
}{\parbox[t]{\linewidth}{\centering Methyl 4-hydroxybenzoate plus more}}
&
\chemname{
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OEt)=-)}
}{Ethyl 4-hydroxybenzoate}
&
\chemname{
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OPr)=-)}
}{\parbox[t]{\linewidth}{\centering Propyl 4-hydroxybenzoate}}
\\
\addlinespace[2ex]
\chemname{
\chemfig{*6((=O)-N(-Me)-*5(-N=-N(-Me)-=)--(=O)-N(-Me)-)}
}{Caffeine}
&
\chemname{
\chemfig{H_2N-[:30](=[:90]S)-[::-60]NH_2}
}{Thiourea}
&
\chemname{
\chemfig{H-C(-[2]H)(-[6]H)-OH}
}{Methanol}
\end{tabularx}
\schemestop

\section{Experimental procedure}

\blindtext

\end{document}


The interaction between \schemestart and tabular bothers me, as they are both doing similar things. This version uses only tabularx and \chemfig.

\documentclass[english]{scrartcl}

\usepackage{babel}
\usepackage{blindtext}
\usepackage{chemfig}
\setchemfig{atom sep=2.25em}
\usepackage{booktabs}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage{showframe}

\begin{document}

\section{Structural formula}

{\def\arraystretch{1.4}% adjust space between figure and text
\begin{tabularx}{\textwidth}{CCC}
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OMe)=-)}
&
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OEt)=-)}
&
\chemfig{*6(=(-OH)-=-(-(=[:150]O)-[:30]OPr)=-)}
\\
Methyl 4-hydroxybenzoate plus more
&
Ethyl 4-hydroxybenzoate
&
Propyl 4-hydroxybenzoate
\\
\addlinespace[2ex]
\chemfig{*6((=O)-N(-Me)-*5(-N=-N(-Me)-=)--(=O)-N(-Me)-)}
&
\chemfig{H_2N-[:30](=[:90]S)-[::-60]NH_2}
&
\chemfig{H-C(-[2]H)(-[6]H)-OH}
\\
Caffeine
&
Thiourea
&
Methanol
\end{tabularx}}

\section{Experimental procedure}

\blindtext

\end{document}