Giter Site home page Giter Site logo

latexdsl's Introduction

LaTeX DSL

latexdsl provides a convenient macro (and very soon a bunch of helper templates / macros) to generate LaTeX code from Nim.

The very short version is, it allows you to write:

let lang = "english"
let res = latex:
  \documentclass{article}
  \usepackage[`lang`]{babel}
  \usepackage[utf8]{inputenc}

to get:

\documentclass{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}

Now this might not seem all that useful by itself “I can just write a string literal with string interpolation”.

Yes, except: Every TeX command either entered using explicit backslash will be checked at compile time against an enum of allowed TeX commands! This means we get compile time checks without having to manually write a very large number of helper templates like the following:

template bold(arg: untyped): untyped = "\textbf{" & $arg & "}"

For limited TeX application in a project that’s certainly enough but definitely not scalable.

The same is done for every command that uses has a statement list as its argument. That’s also where another convenience feature comes in: any Nim block will be understood as a

\begin{command} 
your TeX
\end{command}

command. So the following:

import latexdsl
let res = latex:
  center:
    figure:
      \includegraphics[width=r"0.8\textwidth"]{myImage}
echo res

(Note the usage of raw string literals to work around stuff that isn’t allowed as Nim syntax)

does generate what you would expect:

\begin{center}
\begin{figure}
\includegraphics[width=0.8\textwidth]{myImage}
\end{figure}
\end{center}

Any optional parameter within [] will simply be handed as is into the result. The same is true for arguments within {} or any other command.

The checks are done on:

  • anything starting with a \: \myCommandToBeChecked
  • anything that has opens its own block:
    let res = latex:
      blockCommand[anOption]{anArgument}{anotherArument}:
        "Some random stuff here"
        \checkMe
        

    Here blockCommand and \checkMe will be checked for validity while the other identifiers won’t be.

In case a command is not part of the enum yet, you can omit the CT check by prepending with two \\ instead of one.

Soon to come

Soon there will be convienience features to e.g. turn a number of same length Nim sequences to a LaTeX table or helper templates to create a figure.

In addition to that the compile time checking enum will be extendeable at CT using registerTexCommand.

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.