Pandoc not generating new lines in markdown with latex - latex

I am working on a .md file which includes latex. The file looks like this:
$$
1+1 = 2
\\
2+2 = 4
$$
The File:
When viewing it as markdown the file looks perfectly fine with the new line properly added.
Although when I use pandoc to write the file to a pdf the following happens:
PDF File (from pandoc)
As you can see the new line has been completely removed and makes the latex hard to read.
I am using the following pandoc command:
pandoc --wrap=preserve in.md -o out.pdf
The --wrap=preserve does not seem to be working as it ignores a new line. I have also tried to use \newline \linebreak instead of \\ and neither seem to be working.
How can I specify a line break so pandoc will make sure to keep the breaks rather than keeping everything inline?

Whatever file preview tool you are using: it is lying to you. Double-backslash is not the correct way to insert newlines into math.
Pandoc either parses the math and converts it into the target format, or just passes the code through, depending on the output format. For PDF output via LaTeX, the equation is just passed through. (You can check by running pandoc with --verbose, which, among other things, will print the generated raw LaTeX code.) So it is clear that the problem lies with the input.
There are multiple ways to add linebreaks into math in LaTeX. One of them is the align* environment:
\begin{align*}
1+1 = 2
\\
2+2 = 4
\end{align*}
This will give you the expected PDF output, but has the downside of not showing up in other output formats like HTML. I'm not aware of any method which would produce linebreaks in math equations across all possible pandoc output formats. You'll have to use multiple single-line equations if you need that.

Related

Pandoc re-run latex filter on element after latex+raw_tex and lua filter

I've been writing a custom LaTeX reader Lua filter to convert a bunch of LaTeX source into Pandoc Markdown, which will be my new source for several documents.
I run the usual filter like the following.
pandoc file.tex \
-f latex+raw_tex \
-t markdown \
-o file.md \
--lua-filter myfilter.lua
This works great. I've been able to convert several custom LaTeX environments into Divs and the like.
However, when I have standard LaTeX nested inside the custom environment, the filter output leaves that as-is, unconverted. For example.
\begin{custom_environment}
\begin{itemize}
\item foo
\item bar
\end{itemize}
\end{custom_environment}
The custom environment is handled just fine by my filter, but the internal itemize or tabular or similar are, left unprocessed, as you would expect.
Is there some way to process the contents of the custom environment using the standard latex extension (i.e. filter)? I assume it would be a call from the Lua filter.
An idea I have for doing this that I'm trying to avoid is writing the contents to a temporary file and running another pandoc on that. It seems like such a ubiquitous situation that I'm hoping there's a better way. Thanks!
The best solution I found was pandoc.read:
https://pandoc.org/lua-filters.html#helper-functions
I used it as follows.
internal_div = pandoc.Div(
pandoc.read(
contents_of_custom_environment,
'latex'
).blocks,
'div_label'
)
Now internal_div contains the latex-processed contents of the custom environment. Note that contents_of_custom_environment is just a string.
I tried using walk_block and walk_inline, but couldn't get them to work quite like this. I would welcome an answer that did so.

Render latex preview of equation like in latex export (with preceeding/trailing newline)

I am struggling with latex preview in org-mode.
Basically I am trying to preview someting like this:
* Section2
\[
X=\frac{1}{2}
\]
In general this works, but the overlay image does not contain preceeding/trailing newlines before/after the math environment. Full latex export however would introduce these newlines.
In addition I use the following options in my header in order to keep the original org spacing in my pdf:
#+OPTIONS: \n:t
The combination of employed header and fragment mismatch, however, either messes up readability of my org file, i.e. the preview fragment is put too close to the heading "Section2". Or, when I insert a new line in org, in order to increase the readability, the spacing of my latex export looks strange, due to this extra line plus the preceeding/trailing lines from the math environment.
Can someone give me a hint how I can convince org-mode to create the fragment in accordance with the full latex export?
I am using emacs 26.3 with miktex.
Thanks,
Daniel

Tilde over n when when converting from markdown to latex with pandoc

I have a markdown document that I convert to PDF via pandoc's latex engine. I'm trying to render an n with a tilde over it, as in "niño", with markdown like the following:
ni\~{n}o
...but this just gets rendered in the PDF as "ni~no" -- i.e. the tilde gets interpreted literally. I've also tried escaping the backslash (ni\\~{n}o), surrounding everything in brackets (ni{\~{n}}o), and basically what I think is every possible combination of escaping characters in this sequence, but nothing works. It also fails even when the sequence is on its own (i.e. \~{n}).
But, other similar sequences that are based on letters rather than symbols work just fine (e.g. Otter\r{a} gets rendered correctly to "Otterå"). Pandoc is specifically failing to handle the tilde (or maybe more generally non-letter-based latex character sequences -- I haven't tested others).
The command I'm using to build the pdf is pandoc file.md -o file.pdf. I've also tried specifying -f markdown+raw_tex, but it still fails (nor should I need to, since the \r{a} works without it, and I think raw_tex is enabled by default anyway).
Any thoughts? I know I can use xetex to just enter these characters directly, but that's not really a satisfying solution...
Besides using the ñ character directly (which apparently works in native Pandoc because it's magic!), an alternative is to create a simple LaTeX \newcommand for forcing native TeX interpretation.
\newcommand{\tex}[1]{#1}
ni\tex{\~n}o
Thanks to John McFarlane for introducing me to this clever workaround!

Octave: saving figure with greek letters and subscripts

I'm currently trying to save a stress vs. strain curve using Octave. On this plot, I want to include text showing the equation for calculating engineering stress and engineering strain. Both of these require greek letters (\sigma and \epsilon respectively) as well as subscripts for the formulae.
Currently, using print with -deps, -dpng, or any other device, it creates a file, however the greek letters appear as the words "sigma" and "epsilon", and wherever I have a subscript, such as 0, it just appears as "_0". This looks very unprofessional.
Since I'm generating some 25 graphs, I don't want to have to go through and do a screenshot for each one. Does octave support saving the generated figure as displayed? I intend to use the generated files in a LaTeX document later (preferably as png so I can email them separately too).
I've also tried changing the "graphics_toolkit" option between fltk and gnuplot however it doesn't seem to help.
Attached to this post is a screenshot of the desired results and the actual results.
I am currently "not allowed" to post images, so I'll link them:
http://i.imgur.com/Tjt5Ecn.png (screenshot, desired result) and http://i.imgur.com/SP3hekd.png (directly saved, actual result)
Does anyone know a good way to print a figure from Octave which includes greek characters and subscripts in the titles?
Since you plan to use your graph in a Latex document, generating the graphs with -depslatex and converting them to pdf is a good idea . (Results look slightly better than direct -dpdflatex).
With -depslatex, you can include Latex code in your figures that will be written to a separate tex file.
Note that you need to use double backslashes \\ to export a single backslash.
graphics_toolkit("gnuplot");
...
legend("$\\varepsilon$");
print(sprintf("graph%s_%d.eps", name, type), '-depslatex', '-S200,270', '-F:9');
system(sprintf("epstopdf graph%s_%d.eps", name, type));
On the Latex side, you then \input the tex file generated by Octave. On the plus side, since you need 25 graphs, you can automatize this process on both sides Octave and Latex.
\newcommand{\mygraph}[1]{%
\graphicspath{{./figures/}}
\resizebox{0.495\linewidth}{!}{\relscale{1.0}\small%
\input{./figures/#1.tex}
}%
}
\mygraph{graph1_1}
Here, a Latex command \mygraph is defined to scale and include a figure located in a subfolder.
(I am using Octave 4.0.0 with gnuplot 4.4 on Ubuntu 12)

LaTeX - Listings - Code Indention

so I'm working on some kind of homework paper about git and I want to insert some console output examples. I'm working with TextMate.
I have my LaTeX code indented like every other normal source code, to make it more readable.
My question now is, why get listings in my output pdf indented and how do I prevent that.
Some example code:
\begin{lstlisting}
$ git ls-files
README
TU_Logo_SW.pdf
beleg.pdf
beleg.tex
\end{lstlisting}
In my file there is one tab in front of \begin and two in the lines following.
When I run pdflatex the code will be indented with two tabstops in the pdf. Quickfix is to format all the listings without indention in my tex file, but thats pretty ugly ;-(
lstlisting has a key that lets you remove spaces:
\begin{lstlisting}[gobble=4] will remove the first four characters from every input line in the environment. (I think a tab should still count as one character at that point.)

Resources