Do you have your own LaTeX site? Would you like to add "Open in Overleaf" functionality to help it stand out? If so, you're at the right place!

## One-click to open in Overleaf…

We've developed the tools you need to load LaTeX code and files from your website or blog directly into Overleaf.

See it in action: we recently worked with LaTeX-Community.org to allow all of their 66k examples to be loaded into Overleaf with one click (read more).

## Overview

You can load a code snippet into Overleaf by passing it (or its URI) to https://www.overleaf.com/docs using a form or a link.

The most basic approach is to use a link:

https://www.overleaf.com/docs?snip_uri=http://pathtoyourfile.tex

You may also link to a project instead of a single LaTeX snippet by passing the location of a zip file for the parameter

Here are some links in action:
A LaTeX code snippet - A zipped project

Some more advanced methods of embedding and opening LaTeX content are listed below in the implementations section below. There are also a number of customisable features that can tailor the "Open in Overleaf" functionality to your needs. These can be found in the features section below.

## Implementations

### Post a Snippet by URI (URL)

You can post the URI of a file, and Overleaf will download the file and import it as the main file of the project. Here we pass the full path to this file for the snip_uri parameter. The file must be accessible from our servers. The file will be fetched using either HTTP or HTTPS, depending on the URI that you give it; other protocols are not yet supported. Files should be LaTeX files or zip format.

Here we pass the full path to this file for the snip_uri parameter:

This form can be implemented with the following code:

<form action="https://www.overleaf.com/docs" method="post" target="_blank">
<input type="text" name="snip_uri"
value="https://production-overleaf-static.s3.amazonaws.com/examples/helloworld.tex"><br>
<input type="submit" value="Open in Overleaf">
</form>

Alternatively you can just use a link as described in the Overview section above. We recommend that you use encodeURIComponent, or an equivalent method on the server side to escape each parameter, but this is not absolutely necessary.

You can also upload multiple files using snip_uri[] array parameters like this:

https://www.overleaf.com/docs?snip_uri[]=http://.../a.tex&snip_uri[]=http://.../b.tex

And there's a snip_name parameter that you can use to override the name of each uploaded file, like this:

https://www.overleaf.com/docs?snip_uri[]=http://.../a.tex&
snip_uri[]=http://.../b.tex&
snip_name[]=file1.tex&
snip_name[]=file2.tex

### URL-encoded snippet

URL encoding is useful if you submit using a hidden input field, as in this example. This avoids problems with newlines in the TeX source. This example posts the result of the PHP5 code:

urlencode("\\documentclass{article}\n\\begin{document}\nHello \$i\$ \& \$j\$.\n\\end{document}\n");

in the encoded_snip parameter. If using javascript, the encodeURIComponent function should be used (not the escape function, which has problems with internationalisation).

### Raw snippet

If you submit from the server side or with AJAX you can use the snip parameter, which assumes no encoding. Another use case is submission from a textarea (which could be hidden using CSS); in this case you must escape HTML entities in the HTML source.

This form can be implemented with the following code:

<form action="https://www.overleaf.com/docs" method="post" target="_blank">
<textarea rows="8" cols="60" name="snip">
\usepackage[T1]{fontenc}
\usepackage{amsmath}

\begin{document}
\noindent
Bla bla bla bla :
\begin{align*}
A &amp;= B + C - D \\ \\
%phantom
&amp;\phantom{= B + C \;}
%phantom
+ D - E \\ \\
&amp;= F + G - H.
\end{align*}
\end{document}
</textarea>
<input type="submit" value="Open in Overleaf">
</form>

### Post from Formatted Code Box

This example shows how to extract the unformatted code from a CSS-styled code box and submit it to Overleaf. The key components are:

1. The Javascript that defines the openInOverleaf() function.
2. The form with id ol_form, which is used to POST the source as a URL-encoded snippet. Note that in this case you only need one form on the page, even if you have multiple codeboxes.
3. The link in the code box that actually calls openInOverleaf().

#### Examples

Code: Open in Overleaf
\renewcommand{\arraystretch}{2}
$g :=\left( \begin{array}{c|c} \mathbf{2^D} & \textbf{1} \\ \hline \textbf{1} & \begin{array}{cc} \boldsymbol{1^\alpha} & \textbf{0} \\ \textbf{0} & \boldsymbol{1^\beta} \end{array} \\ \end{array} \right)$
Code: Open in Overleaf
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=3cm,
thick,main node/.style={circle,fill=blue!20,draw,font=\sffamily\Large\bfseries}]

\node[main node] (1) {1};
\node[main node] (2) [below left of=1] {2};
\node[main node] (3) [below right of=2] {3};
\node[main node] (4) [below right of=1] {4};

\path[every node/.style={font=\sffamily\small}]
(1) edge node [left] {0.6} (4)
edge [bend right] node[left] {0.3} (2)
edge [loop above] node {0.1} (1)
(2) edge node [right] {0.4} (1)
edge node {0.3} (4)
edge [loop left] node {0.4} (2)
edge [bend right] node[left] {0.1} (3)
(3) edge node [right] {0.8} (2)
edge [bend right] node[right] {0.2} (4)
(4) edge node [left] {0.2} (3)
edge [loop right] node {0.6} (4)
edge [bend right] node[right] {0.2} (1);
\end{tikzpicture}
\end{document}

#### Implementation

The HTML required to add the examples above is:

<form id="ol_form" action="https://www.overleaf.com/docs" method="post" target="_blank">
<input id="ol_encoded_snip" type="hidden" name="encoded_snip">
</form>
<dl class="codebox">
<dt>Code: <a href="#" onclick="openInOverleaf(this); return false;">Open in Overleaf</a></dt>
<dd>
<pre>
<code class="tex">
\documentclass{article}
...
</code>
</pre>
</dd>
</dl>

The JavaScript code for the openInOverleaf method, which should be included in a script tag is:

function openInOverleaf(a) {
/*
* Get the unformatted code from the formatted code box.
*
* Using the browser's selection isn't ideal, because it clobbers whatever
* the user may have had in their clipboard.
* It's almost possible to use innerText, but that does not work on FF.
* FF supports textContent, but that discards the newlines, which are
* represented by BR tags in the formatted code. So, we have to walk the DOM.
*/
function unformat(e) {
var ret = "";
if (e.nodeType === 1) { // element node
if (e.tagName === "BR") {
return "\n";
} else {
for (e = e.firstChild; e; e = e.nextSibling) {
ret += unformat(e);
}
return ret;
}
} else if (e.nodeType === 3 || e.nodeType === 4) { // text node
return e.nodeValue;
}
}
var code = a.parentNode.parentNode.getElementsByTagName('CODE')[0];
document.getElementById('ol_encoded_snip').value =
encodeURIComponent(unformat(code));
document.getElementById('ol_form').submit();
}

The syntax highlighting in the code boxes is generated by highlight.js, but you can add your own manually with span elements if you prefer.

## Features

The features below are included to aid usability of the "Open in Overleaf" implementations, and may be customized to suit your preferences.

### Decoration

The code must be wrapped with a valid document in order to compile. If the code snippet does not have a \documentclass tag, it is wrapped in a standard document when it is imported:

\documentclass[12pt]{article}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{tikz}
\begin{document}
SNIPPET GOES HERE
\end{document}


### Encoding

The submitted snippet should be encoded using UTF-8. Windows newlines are converted to unix newlines.

### TeX Engine

By default, Overleaf tries to detect which TeX engine to use, and it usually chooses pdflatex. If you would like to set the engine explicitly for your new project, you can pass an engine parameter with one of the following values: latex_dvipdf, pdflatex, xelatex or lualatex.

### Main Document

By default, Overleaf tries to detect which document to use as the main .tex file, based on whether it contains a \documentclass. If you would like to override this behaviour, you can pass a main_document parameter with the name of the file. When supplying an array of snippets, you must also pass an array of filenames for this to work reliably.

### Rich Text

There are two editing modes in Overleaf: Source mode and Rich Text mode. By default, new project are displayed in Source mode.

You can mark a project to default to Rich Text mode by passing a rich_text parameter with value true.

Note that the user can always switch between editing modes with one click, and their preference for each project is stored and has higher precedence over the default setting.

### Comment

We add a short 'welcome' comment at the top of the snippet:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Welcome to Overleaf --- just edit your LaTeX on the left,
% and we'll compile it for you on the right. If you open the
% 'Share' menu, you can invite other users to edit at the same
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%