chemfig

According to others, when drawing planar molecular formula, and even chemical reaction formula, mechanism diagram, everyone uses ChemDraw basically. ChemDraw, of course, is a powerful software that can easily draw both planar and three-dimensional molecular structures. Of course, this power costs money, and for planar formulas or reactions, a free and workable solution is roughly the Chemfig macro package in LaTeX. Chemfig is a macro package developed by French scholars, εTeX, pdfLaTeX and other TeX compilers can be used normally, and the development is relatively active.

texlive

TeX Live is a TeX system released and maintained by THE TUG (TeX User Group), which can be called the official TeX system. For TeX users at any stage, TeX Live can be used to maintain consistency of TeX files across operating systems and users.

Texlive Docker image and service transformation

There are many tutorials in the installation site B of TexLive. The goal is to provide the service of drawing chemFIG chemical equation transformation. However, TexLive software itself does not provide relevant API services, so it needs to be transformed into a service, because considering container deployment, TexLive needs to be packaged into a Docker image

Without further ado, the base image of our choice is TexLive :2020, which uses Python to provide external services. For more information about TexLive, see the blog chemFIG Conversion to PDF

Pull the image and run

docker pull texlive:2020

docker run --name texlive -d texlive:2020

docker ps -a | grep  live
dda1561ae866  texlive:2020   "tail -f /dev/null" 8 seconds ago  Up 2  texlive

docker exec -it dda1561ae866 bash
Copy the code

Install Python and create a Texlive-Python image

# TexLive is based on Alpine Linux and currently mainstreamCat /etc/issue Welcome to Alpine Linux 3.12# Change the APK mirror source
vi etc/apk/repositories
Copy the code

Replace the file content source for ali: mirrors.aliyun.com/alpine/v3.8… Mirrors.aliyun.com/alpine/v3.8…

Update the software libraryApk update the fetch http://mirrors.aliyun.com/alpine/v3.8/main/x86_64/APKINDEX.tar.gz fetch http://mirrors.aliyun.com/alpine/v3.8/community/x86_64/APKINDEX.tar.gz - gf94de196ca v3.8.5-67 [http://mirrors.aliyun.com/alpine/v3.8/main/] v3.8.5-66 - gccbd6a8ae7 [http://mirrors.aliyun.com/alpine/v3.8/community/] OK: 9578 distinct packages available# installation python3
apk add --no-cache python3 python3-dev py-pip

# Verify installationPython3 -v Python 3.6.9 bash-4.4# pip3 -VPIP 18.1 from /usr/lib/python3.6/site-packages (python 3.6)Copy the code

Save the image

docker commit dda1561ae866 textlive-python
Copy the code

At this point, we have a TexLive image of the Python environment

Service transformation

Python scripts

from flask import Flask, abort, request, jsonify
import os
import subprocess
import uuid
import base64

app = Flask(__name__)


@app.route('/texlive/translate/', methods=['POST'])
def translate() :
    if not request.json or 'chemfig' not in request.json:
        abort(400)
    chem_fig = request.json['chemfig']
    # Since it is difficult to calculate the size of the graph of the chemical equation, the paper size is supported here. We default a5paper here
    if 'paper' in request.json:
        paper = request.json['paper']
    else:
        paper = 'a5paper'
    tempFile = open("template.tex")
    lines = tempFile.readlines()
    lines[2] = lines[2].replace("a5paper", paper)
    lines[15] = chem_fig + "\n"
    uuidStr = str(uuid.uuid1())
    new_file_name = uuidStr + '.tex'
    newFile = open(new_file_name, "a+")
    for line in lines:
        newFile.write(line)
    newFile.flush()
    newFile.close()
    try:
        The OS. System can often have a problem with the subprocess. At least the pdflatex command will report an error with the OS
        subprocess.run(["pdflatex"."-interaction=nonstopmode", new_file_name])
        pdf_string = open(uuidStr + '.pdf'."rb").read()
        encoded = base64.b64encode(pdf_string)
        pdf_link = "data:application/pdf; base64,{}".format(encoded)
        pdf_link = pdf_link.replace("b'"."").replace("'"."")
    except:
        remove_file(uuidStr)
    else:
        remove_file(uuidStr)

    return jsonify({"image": pdf_link})


def remove_file(uuid_str) :
    os.system('rm ' + uuid_str + '.tex')
    os.system('rm ' + uuid_str + '.log')
    os.system('rm ' + uuid_str + '.pdf')
    os.system('rm ' + uuid_str + '.aux')


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8080, debug=False)
Copy the code

The Latex template

template.tex

\documentclass{minimal}
\usepackage{xcolor, mol2chemfig}
\usepackage[a5paper, margin=10px, total={6in, 8in}]{geometry}

\usepackage[helvet]{sfmath}
\setcrambond0.4 pt 2.5 pt {} {} {} 1.0 pt\setbondoffset{1pt}
\setdoublesep{2pt}
\setatomsep{%(atomsep)spt}
\renewcommand{\printatom} [1] {\fontsize{8pt}{10pt}\selectfont{\ensuremath{\mathsf{# 1}}}}

\setlength{\parindent}{0pt}
\setlength{\fboxsep}{0pt}
\begin{document}

\chemfig{H_3C-[:30]N**6(-(=O)-(**5(-N(-CH_3)--N-))--N(-CH_3)-(=O)-)}

\end{document}
Copy the code

mol2chemfig.sty

requirements.txt

Flask
Copy the code

startup.sh

python3 ./main.py
Copy the code

DockerFile

FROM texlive-python:2020
COPY main.py /home
COPY mol2chemfig.sty /home
COPY template.tex /home
COPY requirements.txt /home
COPY startup.sh /home
WORKDIR /home
EXPOSE 8080
RUN pip3 install -r requirements.txt && ls
CMD ["bash"."startup.sh"]
Copy the code

Service Image Making

Build texlive – python – API

# dockerfile build
docker build -t texlive-python-api .
# to play tag
docker tag texlive-python-api xxx.xxx.com/xxx/texlive-python-api:2020
# push remote mirror repository
docker push xxx.xxx.com/xxx/texlive-python-api:2020
Copy the code

Try running one

docker run -p 8080:8080 -d --name texlive-python-api texlive-python-api
Copy the code

The request body

{"paper":"a6paper"."chemfig":"\ \ chemfig {CH_3 - N - [:, 108, 1] [:] 54 (- [: 180,0.85,,, the draw = none] \ \ McFcringle {} 1.03) % \ N - N - [: 126] [198] - [: 270] (- \ \ [: 342] the phantom {N })-[:210](=[:270]O)-[:150]N(%\n-[:210,,,2]H_3C)-[:90](=[:150]O)-[:30]N(-[:330])-[:90,,,1]CH_3}\n\n\\bigskip\n\n\\chemfig {CH_3 - N - [:, 108, 1] [:] 54 (- [: 180,0.85,,, the draw = none] \ \ McFcringle {} 1.03) % \ N - N - [: 126] [198] - [: 270] (- \ \ [: 342] the phantom {N}) - [: 210] ( =[:270]O)-[:150]N(%\n-[:210,,,2]H_3C)-[:90](=[:150]O)-[:30]N(-[:330])-[:90,,,1]CH_3}"}
Copy the code

postman Copy out of the browser can be directly accessed, this is a6 paper rendering effect, according to the size of the map can be dynamically passed in the size of the paper And a big one

{"paper":"a3paper"."chemfig":"\ \ chemfig {CH_3 - N - [:, 108, 1] [:] 54 (- [: 180,0.85,,, the draw = none] \ \ McFcringle {} 1.03) % \ N - N - [: 126] [198] - [: 270] (- \ \ [: 342] the phantom {N })-[:210](=[:270]O)-[:150]N(%\n-[:210,,,2]H_3C)-[:90](=[:150]O)-[:30]N(-[:330])-[:90,,,1]CH_3}\n\n\\bigskip\n\n\\chemfig {\ nO % 5 \ n = [: 270] % 3 \ n \ n - [: 210] % 2 (\ n \ n < 150] [: % 4 \ n - [: 90.4, 2.094] 115 \ % n - [: 18] % 109 \ n - [: 330] % 110 \ n - [: 30,,,, DLH] % % 111 \ n - [: 90] 11 2\n-[:150,,,,dlh]%107\n-[:210]%108\n(\n-[:162]\\mcfabove{N}{H}%113\n-[:234]%114\n-[:306,,,,dbl={73}{73}]%->115\n)\n-[:27 0,,,,dlh]%->109\n)\n-[:270,,,2]HN%1\n-[:330,,2]%93\n(\n-[:270]%95\n-[:210]%96\n-[:270]%97\n-[:330]%98\n-[:30]%99\n-[:330 ]%100\n-[:270]%101\n-[:210]%102\n-[:270]%103\n)\n=[:30]O%94\n)\n-[:330]\\mcfbelow{N}{H}%6\n-[:30]%7\n(\n<:[:90]%9\n-[:30 ]%10\n(\n-[:330,,,1]NH_2%12\n)\n=[:90]O%11\n)\n-[:330]%8\n(\n=[:270]O%15\n)\n-[:30]\\mcfabove{N}{H}%13\n-[:330]%14\n(\n< :[:270]%16\n-[:330]%19\n(\n=[:270]O%21\n)\n-[:30,,,1]OH%20\n)\n-[:30]%17\n(\n=[:90]O%22\n)\n-[:330]\\mcfbelow{N}{H}%18\n -[:30]%23\n<:[:90]%24\n(\n=[:150]O%26\n)\n-[:30,,,1]NH%25\n-[:90,,1]%27\n-[:30]%28\n(\n=[:90]O%29\n)\n-[:330]\\mcfbelow{ N}{H}%30\n-[:30]%31\n(\n-[:90]%33\n-[:30]%34\n-[:90]%35\n-[:30,,,1]NH_2%36\n)\n-[:330]%32\n(\n=[:270]O%37\n)\n-[:30]\\mc fabove{N}{H}%38\n-[:330]%39\n(\n<:[:270]%43\n-[:330]%44\n(\n-[:30,,,1]OH%46\n)\n=[:270]O%45\n)\n-[:30]%40\n(\n=[:90]O%42 \n)\n-[:330]\\mcfbelow{N}{H}%41\n-[:30]%47\n(\n<:[:90]%49\n)\n-[:330]%48\n(\n=[:30]O%51\n)\n-[:270,,,2]HN%50\n-[:330,,2] %52\n(\n<[:30]%54\n-[:330]%55\n(\n-[:270,,,1]OH%57\n)\n=[:30]O%56\n)\n-[:270]%53\n(\n=[:330]O%58\n)\n-[:210,,,2]HN%59\n- [:270,,2]%60\n-[:210]%61\n(\n=[:270]O%62\n)\n-[:150]\\mcfabove{N}{H}%63\n-[:210]%64\n(\n<:[:270]%104\n-[:330,,,1]OH%105\ N), n - [: 180,1.732] % 91 \ n \ n - [: 150,,,, DLHS] 92 \ n O %) \ n - [: 270,,, 1] NH 90 \ % n - [:, 210, 1) % (\ n \ n < 82 [: 270] % 84 \ n (\ n - [: 330] % 86 \ n - [: 270] % 87\n(\n-[:210,,,2]HO%89\n)\n=[:330]O%88\n)\n-[:210]%85\n)\n-[:150]%81\n(\n=[:90]O%83\n)\n-[:210]\\mcfbelow{N}{H}%71\n-[: 150]%69\n(\n-[:90]%68\n(\n=[:30]O%70\n)\n-[:150]O%67\n-[:90]%65\n(\n<[:30]%66\n)\n-[:150]%->23\n)\n<[:210]%72\n-[:270]%7 3\n(\n=[:330]O%75\n)\n-[:210]%74\n-[:270,,,,drh]%80\n(\n-[:330,,,1]NH_2%106\n)\n-[:210]%79\n-[:150,,,,drh]%78\n-[:90]%77 \n-[:30,,,,drh]%76\n(\n-[:330]%->74\n)\n}\n"}
Copy the code

PS: LaTeX is a typesetting system based on Ε χ. Not only chemical equations, but also other LaTeX grammars can be rendered through this image. Template. Tex is a template file, which can be customized for LaTeX typesetting

Refer to the link

Chemfig chemical formula into pdf:www.cnblogs.com/xiaoqi/p/ch… Mol2chemfig: chimpsky. Uwaterloo. Ca/mol2chemfig… Chemfig: Chinese manual Chemfig. Man. Huzheyang. Cn/index. The HTML with latex paint organic chemical formula: static.latexstudio.net/wp-content/…