Embedding Pubmed, Graphiviz and a remote image in #LaTeX. My notebook. .
I'm learning LaTeX. Today I learned how to create a new command in LaTeX.
\newcommand{name}[num]{definition}
"Basically the command requires two arguments: the name of the command you want to create, and the definition of the command" . I played with LaTeX and wrote the following three commands:Embedding a remote picture
The following LaTeX document defines a new command "\remoteimage". It takes 3 parameters: a filename, a URL and some parameters for \includegraphics. If the file doesn't exist, the url is downloaded and saved in 'file'. The downloaded image is then included in the final LaTeX document.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass{article} | |
\usepackage{hyperref} | |
\usepackage{graphicx} | |
\newcommand{\remoteimage}[3]{ | |
\IfFileExists{#1}{}{\immediate\write18{curl -o "#1" "#2"}} | |
\begin{center} | |
\includegraphics[#3]{#1} | |
\end{center} | |
} | |
\begin{document} | |
\title{\LaTeX : test with an external Image} | |
\author{Pierre Lindenbaum\\\href{https://twitter.com/yokofakun}{@yokofakun}\\ \url{http://plindenbaum.blogspot.com} } | |
\maketitle | |
\begin{center} | |
\remoteimage{tmp1.jpg}{http://upload.wikimedia.org/wikipedia/commons/2/2e/Charles_Darwin_seated_crop.jpg}{scale=0.1} | |
Charles Darwin. Image via wikipedia. | |
\end{center} | |
\end{document} |
Note: latex files must be compiled with --enable-write18 to enable system-calls.
pdflatex --enable-write18 input.tex
Result:External Image /Latex by lindenb
GraphViz Dot
The second LaTex Document works the same way. It defines a command "\graphviz" , sends the content of the 2nd argument to graphviz dot and save the resulting image before importing it into the LaTeX document.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass{article} | |
\usepackage{hyperref} | |
\usepackage{graphicx} | |
\newcommand{\graphviz}[3]{ | |
\IfFileExists{#1}{}{\immediate\write18{echo #2 | dot -o"#1.png" -Tpng}} | |
\begin{center} | |
\includegraphics[#3]{#1.png} | |
\end{center} | |
} | |
\begin{document} | |
\title{\LaTeX : test with GraphViz} | |
\author{Pierre Lindenbaum\\\href{https://twitter.com/yokofakun}{@yokofakun}\\ \url{http://plindenbaum.blogspot.com} } | |
\maketitle | |
\graphviz{tmpDot}{'digraph G { t9592[label="Gorilla"];t207598[label="Homininae"]; t9606[label="Homo sapiens"]; t9605[label="Homo"];t9604[label="Hominidae"]; t9606 -> t9605; t9605->t207598 ; t9592 -> t207598; t207598->t9604}'}{scale=0.4} | |
\end{document} |
Result:
Pubmed
The last command define "\pmid" . It needs one Pubmed identifer. It downloads the XML record for this pmid, transforms it to LaTeX with xsltproc and the following XSLT stylesheet:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding="ISO-8859-1"?> | |
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0' | |
> | |
<xsl:output method="text" encoding="UTF-8"/> | |
<xsl:template match="/"> | |
<xsl:apply-templates select="PubmedArticleSet/PubmedArticle/MedlineCitation/Article"/> | |
</xsl:template> | |
<xsl:template match="Article"> | |
\section{<xsl:apply-templates select="ArticleTitle"/>} | |
<xsl:apply-templates select="AuthorList"/>\\ | |
\emph{"<xsl:apply-templates select="Journal/Title"/>"}. {\bf vol. <xsl:apply-templates select="Journal/JournalIssue/Volume"/> }. <xsl:apply-templates select="Journal/JournalIssue/Issue"/>. (<xsl:apply-templates select="Journal/JournalIssue/PubDate/Year"/>). \\ | |
<xsl:apply-templates select="Affiliation"/>\\ | |
\url{http://www.ncbi.nlm.nih.gov/pubmed/<xsl:value-of select="../PMID"/>}\\ | |
<xsl:if test="Abstract/AbstractText"> | |
\begin{shaded} | |
<xsl:apply-templates select="Abstract/AbstractText"/> | |
\end{shaded} | |
</xsl:if> | |
</xsl:template> | |
<xsl:template match="AuthorList"> | |
<xsl:text>{\bf </xsl:text> | |
<xsl:for-each select="Author"> | |
<xsl:choose> | |
<xsl:when test="position()=1"></xsl:when> | |
<xsl:when test="position()=last()"> and </xsl:when> | |
<xsl:otherwise>, </xsl:otherwise> | |
</xsl:choose> | |
<xsl:apply-templates select="LastName/text()"/> | |
<xsl:text> </xsl:text> | |
<xsl:apply-templates select="Initials/text()"/> | |
</xsl:for-each> | |
<xsl:text>}</xsl:text> | |
</xsl:template> | |
<!-- copied from https://github.com/Siyavula/siyavula.html2latex/blob/master/templates/xslt/xh2latex.xsl --> | |
<xsl:template match="text()"> | |
<xsl:call-template name="esc"> | |
<xsl:with-param name="c" select='"#"'/> | |
<xsl:with-param name="s"> | |
<xsl:call-template name="esc"> | |
<xsl:with-param name="c" select='"$"'/> | |
<xsl:with-param name="s"> | |
<xsl:call-template name="esc"> | |
<xsl:with-param name="c" select='"%"'/> | |
<xsl:with-param name="s"> | |
<xsl:call-template name="esc"> | |
<xsl:with-param name="c" select='"&"'/> | |
<xsl:with-param name="s"> | |
<xsl:call-template name="esc"> | |
<xsl:with-param name="c" select='"~"'/> | |
<xsl:with-param name="s"> | |
<xsl:call-template name="esc"> | |
<xsl:with-param name="c" select='"_"'/> | |
<xsl:with-param name="s"> | |
<xsl:call-template name="esc"> | |
<xsl:with-param name="c" select='"^"'/> | |
<xsl:with-param name="s"> | |
<xsl:call-template name="esc"> | |
<xsl:with-param name="c" select='"{"'/> | |
<xsl:with-param name="s"> | |
<xsl:call-template name="esc"> | |
<xsl:with-param name="c" select='"}"'/> | |
<xsl:with-param name="s"> | |
<xsl:call-template name="esc"> | |
<xsl:with-param name="c" select='"\"'/> | |
<xsl:with-param name="s" select='.'/> | |
</xsl:call-template> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:template> | |
<xsl:template name="esc"> | |
<xsl:param name="s"/> | |
<xsl:param name="c"/> | |
<xsl:choose> | |
<xsl:when test='contains($s, $c)'> | |
<xsl:value-of select='substring-before($s, $c)'/> | |
<xsl:text>\</xsl:text> | |
<xsl:choose> | |
<xsl:when test='$c = "\"'> | |
<xsl:text>textbackslash </xsl:text> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select='$c'/> | |
</xsl:otherwise> | |
</xsl:choose> | |
<xsl:call-template name="esc"> | |
<xsl:with-param name='c' select='$c'/> | |
<xsl:with-param name='s' select='substring-after($s, $c)'/> | |
</xsl:call-template> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select='$s'/> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
</xsl:stylesheet> |
The LaTeX document includes four pubmed identifiers:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass{article} | |
\usepackage{hyperref} | |
\usepackage{graphicx} | |
\usepackage{framed} | |
\usepackage{color} | |
\usepackage[utf8]{inputenc} | |
\definecolor{shadecolor}{rgb}{0.9,0.9,0.9} | |
\newcommand{\pmid}[1]{ | |
\IfFileExists{pmid_#1.tex}{}{\immediate\write18{curl -s "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=#1&retmode=xml" | xsltproc --novalid -o pmid_#1.tex pubmed2tex.xsl -}} | |
\input{pmid_#1.tex} | |
} | |
\begin{document} | |
\title{\LaTeX : test Pubmed/XSLT} | |
\author{Pierre Lindenbaum\\\href{https://twitter.com/yokofakun}{@yokofakun}\\ \url{http://plindenbaum.blogspot.com} } | |
\maketitle | |
\pmid{22046109} | |
\pmid{21378989} | |
\pmid{21984761} | |
\pmid{15047801} | |
\end{document} |
Result:
That's it,
Pierre