9

I just recently started using LaTeX and chemfig and have a question in regard with making a new line in the text below or above the reaction arrow. I am trying to place MW, 160 °C, 5 min under Na₂CO₃, H₂O as a new line. I will greatly appreciate any help!

0

1 Answer 1

8

You could place the arrow's label's contents in a \parbox:

\documentclass{article}
\usepackage{chemfig}
\begin{document}

\schemestart
 A \arrow{->[\parbox{5cm}{\centering a\\b}][c]} B
\schemestop

\end{document}

enter image description here

or for convenience define a command:

\documentclass{article}
\usepackage{chemfig}

\makeatletter
\newcommand\stack[2][5cm]{\parbox{#1}{\centering#2}}
\makeatother

\begin{document}

\schemestart
 A \arrow{->[\stack{a\\b}][c]} B
\schemestop

\end{document}

The arrow's labels are nodes. If we were able to set add align=center to their properties we would not need the \parbox. Unfortunately chemfig doesn't provide an interface to access their properties directly. The nodes are placed by a command \CF@arrow@display@label@i which we could redefine:

\documentclass{article}
\usepackage{chemfig}

\makeatletter
\def\CF@arrow@display@label@i#1#2#3{%
  \ifx\@empty#1\@empty\else
    \if*\expandafter\@car\detokenize{#1}\@nil
      \ifCF@reac@debug
         node[pos=#2,sloped,yshift=#3\CF@arrow@labelsep,draw,fill,cyan](shifted@node){}%
         node[
           draw,rotate=\CF@rotate@node#1\@nil,anchor=\CF@anchor@node#1\@nil#3,at=(shifted@node),
           align=center% <= this is new
         ]{\expandafter\@gobble\@gobble#1}%
      \else
        node[pos=#2,sloped,yshift=#3\CF@arrow@labelsep](shifted@node){}%
        node[
          rotate=\CF@rotate@node#1\@nil,anchor=\CF@anchor@node#1\@nil#3,at=(shifted@node),
          align=center% <= this is new
        ]{\expandafter\@gobble\@gobble#1}%
      \fi
    \else
      \ifCF@reac@debug
        node[pos=#2,sloped,yshift=#3\CF@arrow@labelsep,draw,fill,cyan](shifted@node){}%
        node[
          draw,pos=#2,anchor=-#390,sloped,yshift=#3\CF@arrow@labelsep,
          align=center% <= this is new
        ]{#1}%
      \else
        node[
          pos=#2,anchor=-#390,sloped,yshift=#3\CF@arrow@labelsep,
          align=center% <= this is new
        ]{#1}%
      \fi
    \fi
  \fi
}
\makeatother

\begin{document}

\schemestart
 A \arrow{->[a\\b][c]} B
\schemestop

\end{document}

The last manual redefinition can be done easier with @egreg's regexpatch package:

\documentclass{article}
\usepackage{chemfig}
\usepackage{regexpatch}
\makeatletter
% \xpatchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>}
% the starred variant replaces all occurrences of <search>
\xpatchcmd*\CF@arrow@display@label@i{node[}{node[align=center,}{}{}
\makeatother

\begin{document}

\schemestart
 A \arrow{->[a\\b][c]} B
\schemestop

\end{document}

You must log in to answer this question.

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