summaryrefslogtreecommitdiff
path: root/graphics/mol2chemfig/latex/mol2chemfig.sty
blob: 67af8f52c5063610063de1b34e3a1e2f395cd71d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
\ProvidesPackage{mol2chemfig}[2019/11/24 v1.5]

\RequirePackage{xcolor, chemfig, twoopt, ifmtarg, calc, xstring}
\usetikzlibrary{decorations}

\def\mcf@three@ea{\expandafter\expandafter\expandafter}

% this macro (by Christian Tellechea) draws circles in aromatic rings. 
\def\mcfcringle#1{% 
 \mcf@three@ea\tikz\mcf@three@ea\draw\mcf@three@ea[\useKV[chemfig]{chemfig
    style}](0,0)circle(\useKV[chemfig]{atom
    sep}*#1*0.66667*\useKV[chemfig]{cycle radius coeff});}

% auxiliary macros for rendering double and triple bonds

% tikz decoration to render parallel bond strokes
\pgfdeclaredecoration{mcfsecondstroke}{initial}{
  \state{initial}[width=\pgfdecoratedpathlength-1sp]{
     \pgfmoveto{\pgfpointorigin}
  }
  \state{final}{
     \pgflineto{\pgfpointorigin}
  }
}

% the complete sines definition is taken from http://tex.stackexchange.com/questions/25678/
\pgfdeclaredecoration{complete sines}{initial}
{
    \state{initial}[
        width=+0pt,
        next state=sine,
        persistent precomputation={\pgfmathsetmacro\matchinglength{
            \pgfdecoratedinputsegmentlength / int(\pgfdecoratedinputsegmentlength/\pgfdecorationsegmentlength)}
            \setlength{\pgfdecorationsegmentlength}{\matchinglength pt}
        }] {}
    \state{sine}[width=\pgfdecorationsegmentlength]{
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathsine{\pgfpoint{0.25\pgfdecorationsegmentlength}{-0.5\pgfdecorationsegmentamplitude}}
        \pgfpathcosine{\pgfpoint{0.25\pgfdecorationsegmentlength}{0.5\pgfdecorationsegmentamplitude}}
}
    \state{final}{}
}

% color definitions 
\colorlet{mcfbgcolor}{white} % adjust when drawing on a non-white canvas 
\colorlet{mcfpusharrowcolor}{black}
\colorlet{mcfatomnocolor}{blue}

\def\CF@@keytomacro#1#2{\edef#1{\unexpanded\expandafter\expandafter\expandafter{\useKV[chemfig]{#2}}}}

\tikzset{ % set styles
    % wavy bonds. Slightly modified from http://www.texdev.net/tag/chemfig/
    setatomsep/.code={
      \CF@@keytomacro\CF@@atomsep{atom sep}
      \pgfmathsetmacro\mcf@@segmentlength{\CF@@atomsep/6}
      \pgfmathsetmacro\mcf@@amplitude{\CF@@atomsep/10}
    },
    %
    mcfwavy/.style =
    {
      setatomsep,
      decorate,
      decoration =
        {
          complete sines,
          post length = 0 pt,
          pre length  = 0 pt,
          % Use chemfig's atom spacing
          segment length=\mcf@@segmentlength, 
          amplitude=\mcf@@amplitude
        }
    },
    % this style is used by the \mcfelmove macro
    mcfpusharrow/.style = {
        ->,  
        mcfpusharrowcolor,
        >=stealth,  
        shorten <=3pt,
        shorten >=2pt,
        preaction={draw=mcfbgcolor, -,line width=1.5pt}
    },
    %
    % one bond crossing before another - gets a white backdrop 
    % as wide as doublesep
    mcfbgcolor/.style={white},
    %
    mcfx/.style 2 args={%
        preaction={
            draw,
            mcfbgcolor,
            line width={\useKV[chemfig]{double bond sep}},
            shorten <= {#1*\useKV[chemfig]{double bond sep}/100},
            shorten >= {#2*\useKV[chemfig]{double bond sep}/100}
        }
    },
    %
    mcfcrossbond/.style={% for backwards compatibility only
        mcfx={100}{100}
    },
    %
    secondbond/.style 2 args={
        shorten <= {#1*\useKV[chemfig]{double bond sep}/100}, 
        shorten >= {#2*\useKV[chemfig]{double bond sep}/100}
    },
    %
    secondfgbond/.style 2 args={
        preaction={draw,-,mcfbgcolor,line width={\useKV[chemfig]{double bond sep}}},
        secondbond={#1}{#2}    
    },
    %
    secondleft/.style 2 args={
        secondbond={#1}{#2},
        decoration={mcfsecondstroke,raise={\useKV[chemfig]{double bond sep}}},
        decorate
    },
    %
    secondright/.style 2 args={
        secondbond={#1}{#2},
        decoration={mcfsecondstroke,raise=-{\useKV[chemfig]{double bond sep}}},
        decorate
    },
    %
    secondfgleft/.style 2 args={
        secondfgbond={#1}{#2},
        decoration={mcfsecondstroke,raise={\useKV[chemfig]{double bond sep}}},
        decorate
    },
    %
    secondfgright/.style 2 args={
        secondfgbond={#1}{#2},
        decoration={mcfsecondstroke,raise=-{\useKV[chemfig]{double bond sep}}},
        decorate
    },
    %
    dbl/.style 2 args={
      preaction={draw,secondleft={#1}{#2}}
    },
    %
    dbr/.style 2 args={
      preaction={draw,secondright={#1}{#2}}
    },
    %
    trpl/.style 2 args={
      preaction={draw,secondleft={#1}{#2}},
      preaction={draw,secondright={#1}{#2}}
    },
    %
    dblx/.style n args={4}{
      preaction={draw,secondfgleft={#1}{#2}},
      mcfx={#3}{#4}
    },
    %
    dbrx/.style n args={4}{
      preaction={draw,secondfgright={#1}{#2}},
      mcfx={#3}{#4}
    },
    %
    trplx/.style n args={4}{
      preaction={draw,secondfgleft={#1}{#2}},
      preaction={draw,secondfgright={#1}{#2}},
      mcfx={#3}{#4}
    },
    %
    drh/.style={dbr={58}{58}},
    dlh/.style={dbl={58}{58}},
    drhs/.style={dbr={58}{0}},
    dlhs/.style={dbl={58}{0}},
    drhe/.style={dbr={0}{58}},
    dlhe/.style={dbl={0}{58}},
    drn/.style={dbr={0}{0}},
    dln/.style={dbl={0}{0}}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Conveniences for drawing push arrows
% the \mcfelmove command is adapted from the
% myChemistry package by Clemens Niederberger

\newcommand{\mcfelmove}[5][]{%
\node at (0,0) {% dummy wrapper node
\chemmove{\draw[mcfpusharrow,#1](#2).. controls +(#3) and +(#5)..(#4);%
}};}

% and, since the most common use for tikz code
% is arrow shortening and lengthening: a command streamlined for that.

\newcommandtwoopt{\mcfpush}[6][][]{% we use empties to keep same defaults
\@ifmtarg{#2}{
    \@ifmtarg{#1}{\mcfelmove{#3}{#4}{#5}{#6}}
                 {\mcfelmove[shorten <=#1]{#3}{#4}{#5}{#6}}
}{% #2 not empty
    \@ifmtarg{#1}{\mcfelmove[shorten >=#2]{#3}{#4}{#5}{#6}}
                 {\mcfelmove[shorten <=#1, shorten >=#2]{#3}{#4}{#5}{#6}}
}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Stacking atoms horizontally and vertically
\newlength{\mcfvspace}
\setlength{\mcfvspace}{1.5pt}

\newcommand{\mcfabove}[2]{\chemabove[\mcfvspace]{#1}{#2}}
\newcommand{\mcfbelow}[2]{\chembelow[\mcfvspace]{#1}{#2}}

\newlength{\mcf@boxwidth}
\newlength{\mcf@boxheight}

\newcommand{\mcf@box}[3][l]{
\settowidth{\mcf@boxwidth}{\printatom{#2}}
\settoheight{\mcf@boxheight}{\printatom{#2}}
\makebox[\mcf@boxwidth][#1]{\raisebox{0pt}[\mcf@boxheight][0pt]{\printatom{#3}}}}

\newcommand{\mcfleft}[2]{\mcf@box[r]{#2}{#1#2}}
\newcommand{\mcfright}[2]{\mcf@box{#1}{#1#2}}

\newcommand{\mcfaboveright}[3]{\mcfabove{#1}{\mcf@box{#2}{#2#3}}}
\newcommand{\mcfbelowright}[3]{\mcfbelow{#1}{\mcf@box{#2}{#2#3}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Formatting of charges and atom numbers

% Charges - could for example be defined as \oplus and \ominus
\newcommand{\mcfplus}{+}
\newcommand{\mcfminus}{-}

% atom numbers (used when option atom-numbers is selected)
\newcommand{\mcfatomno}[1]%
{\raisebox{2pt}{\color{mcfatomnocolor}{\ensuremath{\mathsf{_{#1}}}}}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% a little help with direct processing of mol2chemfig commands
\newcommand{\mcfinput}[1]{\@@input|"mol2chemfig #1"}