Coder Social home page Coder Social logo

ob-latex-as-png's Introduction

An Org-babel “language” whose execution produces PNGs from LaTeX snippets; useful for shipping arbitrary LaTeX results in HTML or to inline such snippets in Emacs! (•̀ᴗ•́)و

badge:Emacs badge:Org

MELPA

badge:license badge:https://github.com/alhassy/ob-latex-as-png badge:contributions

badge:author badge:

;; Installation
(use-package ob-latex-as-png
  :ensure t)

Table of Contents

  1. Screenshot 1: Fancy Writing
  2. Screenshot 2: Bubble Diagrams
  3. Screenshot 3: Fine-grained Size Control
  4. Screenshot 4: Local LaTeX Header Matter
  5. Hint: Always redisplay images after C-c C-c
  6. Enjoy!

Screenshot 1: Fancy Writing

img

🌹 Click to see Source!
#+PROPERTY: header-args:latex-as-png :results raw value replace
#+begin_src latex-as-png
\input GoudyIn.fd
\def\fncy#1{\fontsize{50}{60}\selectfont{\usefont{U}{GoudyIn}{xl}{n} #1}}

\hspace{0.15\textwidth}\fncy{EMACS}
\newline
\fncy{ORG}\raisebox{0.5em}{$\sim$}\fncy{MODE}
#+end_src

Note: The Goudy Initalen font exists in uppercase only.

Screenshot 2: Bubble Diagrams

img

🚀 Click to see Source!
#+PROPERTY: header-args:latex-as-png :results raw value replace
#+begin_src latex-as-png :file example.pdf :resolution 120
   \smartdiagram[bubble diagram]{Emacs,Org-mode, \LaTeX, Pretty Images, HTML}
#+end_src

Note: The extension given to :file does not matter, only the base name is used and a .png is produced. As such, extensions are completely optional (and discarded when provided).

Screenshot 3: Fine-grained Size Control

Instead of using :resolution as in the previous screenshot, you can get fine grained control of the size of the resulting PNG by using LaTeX's picture environment.

img

🌱 Click to see Source! 🗝
#+begin_src latex-as-png :file using-picture-environment
\input GoudyIn.fd \input Acorn.fd
\def\light#1{\huge \usefont{U}{Acorn}{xl}{n} #1}
\def\thick#1{\fontsize{50}{60}\selectfont \usefont{U}{GoudyIn}{xl}{n} #1}
% \fontsize{X}{Y} ⇒ Write with point size X, using Y point size between lines

\pagecolor{brown}

\begin{picture}(124,60)
\put(0,0){\thick{ORG}}
\put(18,42){\light{EMACS}}
\put(0,55){\rotatebox{-45}{\scriptsize \LaTeX}}
\put(109,43){\rotatebox{45}{\scriptsize \texttt{HTML}}}
\end{picture}
#+end_src

Screenshot 4: Local LaTeX Header Matter

You can add arbitrary LaTeX header matter —i.e., the stuff before LaTeX's \begin{document}— by using having a line with just “ % in ” to separate the header from the main PNG body.

🐺 Customisation and Global Headers 🗝
(defvar ob-latex-as-png-header '("\\usepackage{smartdiagram}")
  "The LaTeX preamble used for executing latex-as-png source blocks.

This is generally any LaTeX matter that may appear before \\begin{document}.")


(defvar ob-latex-as-png-header-separator "% in"
  "A literal expression that separates local LaTeX header matter from the body.

Everything before the separator is matter that is necessary
to produce a PNG from the primary LaTeX.")

img

🌱 Click to see Source! 🗝
#+begin_src latex-as-png :results replace :file neural-networks
\usepackage{neuralnetwork}
% in
\begin{neuralnetwork}[height=4]
  \newcommand{\x}[2]{$x_#2$}
  \newcommand{\y}[2]{$y_#2$}
  \newcommand{\hfirst}[2]{\small $h^{(1)}_#2$}
  \newcommand{\hsecond}[2]{\small $h^{(2)}_#2$}
  \newcommand{\mylinktext}[4] {
    % from layer=#1, from node=#2
    % to layer=#3, to node=#4
  \ifnum1=#1\relax
     \ifnum3=#4\relax $w^{#1}_{#4,#2}$ \fi
  \else \fi
  }
  % Then assign it:
  \setdefaultlinklabel{\mylinktext}
  \inputlayer[count=3, bias=false, title=Input\\layer, text=\x]
  \hiddenlayer[count=4, bias=false, title=Hidden\\layer 1, text=\hfirst] \linklayers
  \hiddenlayer[count=3, bias=false, title=Hidden\\layer 2, text=\hsecond] \linklayers
  \outputlayer[count=2, title=Output\\layer, text=\y] \linklayers
\end{neuralnetwork}
#+end_src

( Learn more about the neural networks LaTeX library here. )

Hint: Always redisplay images after C-c C-c

Place the following incantation in your init (•̀ᴗ•́)و)

;; Always redisplay images after C-c C-c (org-ctrl-c-ctrl-c)
(add-hook 'org-babel-after-execute-hook 'org-redisplay-inline-images)

Enjoy!

Thank you

ob-latex-as-png's People

Contributors

alhassy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ob-latex-as-png's Issues

File paths containing "." cause export errors

I'm not sure that string splitting on "." here is the right move to get the file path without extension:

(file (car (split-string file.ext "\\.")))

If I use a path like ../img/whatever.png the filename is just the empty string, which is not ideal. Locally I replaced this binding with (file-name-sans-extension file.ext) and it seems to work much better.

Files should not clutter user-emacs-directory

  • Every operation that I perform creates three files
    {tex,png,pdf}, which are all stored in my ~/.emacs.d/
    directory (variable user-emacs-directory).

  • I was expecting any created files to be stored elsewhere:

    • in order to avoid cluttering

    • in order to tightly associate the files to its parent org file.

  • Some options that would make sense to me:

    • A dedicated subfolder of 'user-emacs-directory'. The disadvantage
      of this option is that it collects files from many parent org
      files in a single location, ie. no tight association between
      parent org file and children {tex,png,pdf} files.

    • A dedicated sub-folder at the org-file's location, possibly with a
      naming convention that includes the org-file's name. For example,
      files created for /home/alice/Documents/foo.org would be
      stored in sub-folder /home/alice/Documents/foo_org_ob_files/.

  • I wasn't expecting so many files to persist, when only the png file
    is used. The other two files seem like clutter that should be
    automatically deleted.

  • I'm a brand new user to your package. I think it's a great
    contribution to Emacs and org-mode. Thanks for sharing it with
    everyone.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.