diff options
author | Karl Berry <karl@freefriends.org> | 2022-10-09 20:18:38 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2022-10-09 20:18:38 +0000 |
commit | 5779da34ced377949a78789c841837b9d363c90a (patch) | |
tree | a901e7e088f147e32012175d9494dbc0750f512b /Master/texmf-dist/tex/latex/chemobabel | |
parent | 9d551b24b421b2b832adda5d54a5ec0e6db99934 (diff) |
chemobabel (9oct22)
git-svn-id: svn://tug.org/texlive/trunk@64665 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/latex/chemobabel')
-rw-r--r-- | Master/texmf-dist/tex/latex/chemobabel/chemobabel.sty | 230 |
1 files changed, 193 insertions, 37 deletions
diff --git a/Master/texmf-dist/tex/latex/chemobabel/chemobabel.sty b/Master/texmf-dist/tex/latex/chemobabel/chemobabel.sty index 4f3122bb37a..070effec11d 100644 --- a/Master/texmf-dist/tex/latex/chemobabel/chemobabel.sty +++ b/Master/texmf-dist/tex/latex/chemobabel/chemobabel.sty @@ -17,7 +17,7 @@ \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{chemobabel} - [2022/09/19 v0.9j Chemical structures with Open Babel] + [2022/10/09 v0.9k Chemical structures with Open Babel] %% Start of package main code %% Thanks: http://imada.sdu.dk/~jlandersen/latex/graphvizObabel.sty @@ -25,6 +25,9 @@ \RequirePackage{verbatim} \RequirePackage{graphicx} +%% ----- Check status ----- +\chardef\chemob@bel@status\z@ + %% ----- External command execution ----- \ifx\directlua\@undefined \def\chemob@bel@exec#1{\immediate\write18{#1}} @@ -44,6 +47,17 @@ \chardef\chemob@bel@shellescape =\directlua{tex.sprint(status.shell_escape)} \fi +%% ----- Ensure -shell-escape when processing ChemFigFile.tex +\edef\chemob@bel@jobname{\jobname} +{\escapechar=-1 \xdef\chemob@bel@outname{\string\ChemFigFile}} +\ifx\chemob@bel@jobname\chemob@bel@outname + \ifnum\chemob@bel@shellescape=\@ne\else + \PackageError{chemobabel}{% + Processing \chemob@bel@outname.tex needs -shell-escape.\MessageBreak + Run 'pdflatex -shell-escape \chemob@bel@outname.tex'}\@ehc + \fi +\fi + %% ----- Make a directory for images ----- \newcommand\chemobabelimgdir{chemobabelimgdir} \chemob@bel@exec{mkdir \chemobabelimgdir} @@ -98,6 +112,7 @@ No log file.% }% \end{minipage}}% + \global\chardef\chemob@bel@status\@ne }% } %% ----- Common part of \smilesobabel and \chemobabel end ----- @@ -111,14 +126,28 @@ % 1: (optional) options for includegraphics % 2: SMILES notation % 3: options for obabel -\newcommand\smilesobabel[1][scale=1]{% +\newcommand\smilesobabel{\@ifstar{\smilesobabel@i}{\smilesobabel@ii}} +\newcommand\smilesobabel@i[1][scale=1]{% \def\smilesobabel@next{\includegraphics[#1]}% \begingroup \let\do\@makeother \dospecials \catcode`\{=1 \catcode`\}=2 - \@smilesobabel + \@smilesobabel@i } -\newcommand\@smilesobabel[2]{% +\newcommand\smilesobabel@ii[1][scale=1]{% + \def\smilesobabel@next{\includegraphics[#1]}% + \begingroup + \let\do\@makeother \dospecials \catcode`\{=1 \catcode`\}=2 + \@smilesobabel@ii +} +\def\@smilesobabel@i#1{% + \endgroup + \@smilesobabel@main{#1}{}% +} +\def\@smilesobabel@ii#1#2{% \endgroup + \@smilesobabel@main{#1}{#2}% +} +\def\@smilesobabel@main#1#2{% \chemob@bel@exec{% \smilesob@bel@obabelcmd{#1}{#2} 2>\smilesob@belGetName.log && \chemob@bel@svgtoimgcmd{\smilesob@belGetName}{\chemob@belimgExt} 2>>\smilesob@belGetName.log @@ -138,14 +167,28 @@ % 1: (optional) options for includegraphics % 2: original files (.mol, .cdx, etc.) % 3: options for obabel -\newcommand\chemobabel[1][scale=1]{% +\newcommand\chemobabel{\@ifstar{\chemobabel@i}{\chemobabel@ii}} +\newcommand\chemobabel@i[1][scale=1]{% + \def\chemobabel@next{\includegraphics[#1]}% + \begingroup + \let\do\@makeother \dospecials \catcode`\{=1 \catcode`\}=2 + \@chemobabel@i +} +\newcommand\chemobabel@ii[1][scale=1]{% \def\chemobabel@next{\includegraphics[#1]}% \begingroup \let\do\@makeother \dospecials \catcode`\{=1 \catcode`\}=2 - \@chemobabel + \@chemobabel@ii +} +\def\@chemobabel@i#1{% + \endgroup + \@chemobabel@main{#1}{}% } -\newcommand\@chemobabel[2]{% +\def\@chemobabel@ii#1#2{% \endgroup + \@chemobabel@main{#1}{#2}% +} +\def\@chemobabel@main#1#2{% \IfFileExists{"#1"}{% % the file exists: start processing % (``#1'' can contain spaces, so ``"'' required) @@ -157,7 +200,7 @@ \chemob@bel@common@maybeimg{\chemobabel@next}{\chemob@belGetName}{file ``#1''}% }{% % the file does not exist: show a std error - \PackageError{chemobabel}{File ``#1'' not found}{}% + \PackageError{chemobabel}{File ``#1'' not found}\@ehc \fbox{\begin{minipage}{0.9\textwidth} Error in chemobabel: the file ``#1'' does not exist. \end{minipage}}% @@ -201,67 +244,180 @@ \DeclareOption{librsvg}% {\let\chemob@bel@svgtoimgcmd\chemob@bel@librsvgcmd} % crop image or not -\DeclareOption{nocrop}{% - \let\chemob@bel@cropcmd\@gobble - \let\chemob@bel@maybecrop\@empty} +\let\chemob@bel@nocrop\relax +\DeclareOption{nocrop}{\let\chemob@bel@nocrop\@empty} % default settings \ExecuteOptions{pdf,inkscape} -\ProcessOptions\relax +% handle user settings - arbitrary order allowed +\ProcessOptions*\relax +\ifx\chemob@bel@nocrop\relax \else + \let\chemob@bel@cropcmd\@gobble + \let\chemob@bel@maybecrop\@empty +\fi %% ----- Declaration of options end ----- +%% ----- Warn if something went wrong ----- +% e.g.) \chemob@bel@common@maybeimg is not an image +% --- when current processing failed +% AND previous shell-escape run not found +% e.g.) \chemob@bel@common@maybeimg is definitely a wrong image +% --- when current processing failed +% BUT previous shell-escape run with different total number found +% --- note that re-run after removing [extract] should avoid the warning +% --- as it will include all the correct images from previous shell-escape run +\def\chemob@bel@record@imgnum#1{% + \immediate\write#1{\gdef\string + \smilesob@bel@imgNum{\number\value{smilesob@belCounter}}}% + \immediate\write#1{\gdef\string + \chemob@bel@imgNum{\number\value{chemob@belCounter}}}% +} +\def\chemob@bel@preserve@imgnum#1{% + \immediate\write#1{\gdef\string + \smilesob@bel@imgNum{\smilesob@bel@imgNum}}% + \immediate\write#1{\gdef\string + \chemob@bel@imgNum{\chemob@bel@imgNum}}% +} +\def\chemob@bel@atenddocument{% + \ifnum\chemob@bel@status>\z@ % at least one is not an image + \PackageWarningNoLine{chemobabel}{% + Some processing failed.\MessageBreak + Please rerun}% + \else % all images + \ifnum\chemob@bel@shellescape=\@ne % current run is shell-escape + % record number of images of the current run + \if@filesw + \chemob@bel@record@imgnum{\@mainaux}% + \fi + % when processing ChemFigFile.tex, also write to the original .aux + \ifx\chemob@bel@jobname\chemob@bel@outname + \IfFileExists{\chemobabelfile.tex}{% + \if@filesw + \newwrite\chemob@bel@origaux + \immediate\openout\chemob@bel@origaux=\chemobabelfile.aux\relax + \chemob@bel@record@imgnum{\chemob@bel@origaux}% + \immediate\closeout\chemob@bel@origaux + \fi + }{}% + \fi + \else + % compare number of images with the previous shell-escape run + \ifx\smilesob@bel@imgNum\@undefined\def\smilesob@bel@imgNum{0}\fi + \ifnum\value{smilesob@belCounter}=\smilesob@bel@imgNum\relax\else + \PackageWarningNoLine{chemobabel}{% + Number of \noexpand\smilesobabel changed.\MessageBreak + Please rerun}% + \fi + \ifx\chemob@bel@imgNum\@undefined\def\chemob@bel@imgNum{0}\fi + \ifnum\value{chemob@belCounter}=\chemob@bel@imgNum\relax\else + \PackageWarningNoLine{chemobabel}{% + Number of \noexpand\chemobabel changed.\MessageBreak + Please rerun}% + \fi + % record number of images of the previous shell-escape run + \if@filesw + \chemob@bel@preserve@imgnum{\@mainaux}% + \fi + \fi + \fi +} +\AtEndDocument{\chemob@bel@atenddocument} +%% ----- Warn if something went wrong end ----- + %% ----- Exit this package now, if [extract] is not specified ----- \ifx\chemob@bel@extract\relax \endinput \fi %% Extracting all codes of Open Babel figures %% Thanks: http://oku.edu.mie-u.ac.jp/tex/mod/forum/discuss.php?d=1411 +%% ----- Safety check before opening output file ----- +\ifx\chemob@bel@jobname\chemob@bel@outname + \PackageWarningNoLine{chemobabel}{% + Wow! Your file name is `\chemob@bel@outname'!\MessageBreak + Option [extract] ignored to avoid overwriting} + \expandafter\endinput +\fi + \PackageWarningNoLine{chemobabel}{% You are using [extract] option.\MessageBreak No chemical structures will be printed} +%% ----- Accumulate user-given options to be passed ----- +% the image extension (non-empty) +\edef\chemob@bel@options{\chemob@belimgExt} +% image conversion command (except default=inkscape) +\ifx\chemob@bel@svgtoimgcmd\chemob@bel@inkscapeoldcmd + \edef\chemob@bel@options{\chemob@bel@options,inkscape-old} +\else\ifx\chemob@bel@svgtoimgcmd\chemob@bel@librsvgcmd + \edef\chemob@bel@options{\chemob@bel@options,librsvg} +\fi\fi +% crop image or not +\ifx\chemob@bel@nocrop\relax \else + \edef\chemob@bel@options{\chemob@bel@options,nocrop} +\fi + %% ----- Define intermediate output file and load packages ----- -\newwrite\ChemFigFile -\immediate\openout\ChemFigFile=ChemFigFile.tex\relax -\immediate\write\ChemFigFile{\string\documentclass{article}} -\immediate\write\ChemFigFile{\string\usepackage{graphicx}} -\immediate\write\ChemFigFile{\string\usepackage{chemobabel}} +\newwrite\chemob@bel@outfile +\immediate\openout\chemob@bel@outfile=\chemob@bel@outname.tex\relax +\immediate\write\chemob@bel@outfile{\string\def\string\chemobabelfile{\chemob@bel@jobname}} +\immediate\write\chemob@bel@outfile{\string\documentclass{article}} +\immediate\write\chemob@bel@outfile{\string\usepackage[\chemob@bel@options]{chemobabel}} %% ----- Read and write ----- -\immediate\write\ChemFigFile{\string\begin{document}} -\AtEndDocument{% - \immediate\write\ChemFigFile{\string\end{document}}% - \immediate\closeout\ChemFigFile +\immediate\write\chemob@bel@outfile{\string\begin{document}} +\def\chemob@bel@atenddocument{% + \immediate\write\chemob@bel@outfile{\string\end{document}}% + \immediate\closeout\chemob@bel@outfile \chemob@bel@ifnochem{}{% \PackageWarningNoLine{chemobabel}{% Some chemical structures are extracted.\MessageBreak Run 'pdflatex -shell-escape ChemFigFile.tex'}}% } -\renewcommand\smilesobabel{% +% \smilesobabel*[#1]{#2} => normalized to \smilesobabel[#1]{#2}{#3} +\renewcommand\smilesobabel@i{% + \begingroup + \let\do\@makeother \dospecials \catcode`\{=1 \catcode`\}=2 + \@smilesobabel@i +} +\renewcommand\smilesobabel@ii{% \begingroup - \let\do\@makeother - \dospecials - \catcode`\{=1 - \catcode`\}=2 - \@@smilesobabel + \let\do\@makeother \dospecials \catcode`\{=1 \catcode`\}=2 + \@smilesobabel@ii } -\newcommand\@@smilesobabel[3][scale=1]{% +\renewcommand\@smilesobabel@i[2][scale=1]{% \endgroup - \immediate\write\ChemFigFile{% + \@smilesobabel@main{#1}{#2}{}% +} +\renewcommand\@smilesobabel@ii[3][scale=1]{% + \endgroup + \@smilesobabel@main{#1}{#2}{#3}% +} +\def\@smilesobabel@main#1#2#3{% + \immediate\write\chemob@bel@outfile{% \string\smilesobabel[#1]{#2}{#3}% \string\newpage}% [\smilesob@belGetName.\chemob@belimgExt]% \addtocounter{smilesob@belCounter}{1}} -\renewcommand\chemobabel{% +% \chemobabel*[#1]{#2} => normalized to \chemobabel[#1]{#2}{#3} +\renewcommand\chemobabel@i{% \begingroup - \let\do\@makeother - \dospecials - \catcode`\{=1 - \catcode`\}=2 - \@@chemobabel + \let\do\@makeother \dospecials \catcode`\{=1 \catcode`\}=2 + \@chemobabel@i } -\newcommand\@@chemobabel[3][scale=1]{% +\renewcommand\chemobabel@ii{% + \begingroup + \let\do\@makeother \dospecials \catcode`\{=1 \catcode`\}=2 + \@chemobabel@ii +} +\renewcommand\@chemobabel@i[2][scale=1]{% \endgroup - \immediate\write\ChemFigFile{% + \@chemobabel@main{#1}{#2}{}% +} +\renewcommand\@chemobabel@ii[3][scale=1]{% + \endgroup + \@chemobabel@main{#1}{#2}{#3}% +} +\def\@chemobabel@main#1#2#3{% + \immediate\write\chemob@bel@outfile{% \string\chemobabel[#1]{#2}{#3}% \string\newpage}% [\chemob@belGetName.\chemob@belimgExt]% |