Converting from latex to markdown with pandoc, images not converted - latex

I'm trying to convert a latex file to markdown using Pandoc. The latex file contains linked images, for example like this:
\begin{figure}[htbp]
\begin{center}
\leavevmode
\scalebox{0.4}{\includegraphics{microsatsFigures/Figure_traceView.png}}
\end{center}
\caption{The Traces view}
\label{traces}
\end{figure}
However in the markdown file all I get is
::: center
:::
Is this a known limitation of Pandoc, or is there some command I'm missing that will convert the image instructions? The command I used in pandoc was just the standard format:
pandoc -f latex -t markdown Inputfile.tex -o Outputfile.md

Related

How to solve error with YAML, Preamble (LaTex) and Pandoc conversion

I started using markdown together with pandoc a few weeks ago. I'm especially fond of the markdown editor writemonkey v.3! I have one slight problem with converting my .md to .pdf, and I it's caused by the first line in my document. The first line in the document (see below) is how you give the document a name in writemonkey.
name: seminarium 1
---
author: my name
date: 22 Augusti 2020
bibliography: testlibrary.bib
geometry:
- top=2.5cm
- bottom=2.5cm
- left=2.5cm
- right=2.5cm
pagesize: a4
font: Times New Roman
fontsize: 12pt
header-includes:
- \usepackage{setspace}
- \setstretch{1.0}
- \pagenumbering{gobble}
---
If I remove this first line, the pandoc conversion works like a charm. Otherwise I get this:
pandoc sem1.md -o sem1.pdf --bibliography library.bib -V lang=sv
Error producing PDF.
! LaTeX Error: Can be used only in preamble.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.78 \usepackage
Now - I realize that I've combined a YAML with LaTex code, I am hoping that my formatting is "bad" and that I might improve it so I don't have to remove the first line every time. Maybe tell Pandoc/LaTex to ignore the first line or make a separate .yaml file would help?
Ok! This what I did. Works great with Writemonkey. Just copy-paste in Notepad and save as a .ps1 file and run in Powershell!
CD C:\Users\username\Desktop\ppp2020 # Filepath of .md, .yaml and .bib
Get-Content cap.md -Encoding UTF8 | Select-Object -Skip 2 | Set-Content cap_temp.md -Encoding UTF8 # Removes the first line (name:) from markdown document
pandoc cap_temp.md default.yaml -o cap.pdf --bibliography library.bib -V lang=sv # converts .md to .pdf
Remove-Item 'C:\Users\username\Desktop\ppp2020\cap_temp.md' # This removes the tempfile

Setting otherlangs from the command line using pandoc

Using pandoc, I managed to produce the following output using the YAML prolog.
---
lang: fr
otherlangs: [en]
---
Generates this latex code.
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[shorthands=off,english,main=french]{babel}
\else
\usepackage{polyglossia}
\setmainlanguage[]{french}
\setotherlanguage[]{english}
\fi
But, I cannot manage to have it working using the commande-line arguments. This what I tried.
$ pandoc -s -t latex -V lang=fr -V otherlangs="[en]"
Which produces:
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[shorthands=off,main=french]{babel}
\else
\usepackage{polyglossia}
\setmainlanguage[]{french}
\fi
Any hints on how I should pass de otherlangs variable from the command line?
AFAIK this is not possible since the -M option (and probably -V as well) parse values
as YAML boolean or string values
and otherlangs needs to be a list. What's your use case?
You shouldn't have to set otherlangs manually as it's extracted from spans and divs with the lang attribute (see the MANUAL), for example:
my [english words]{lang=en} inside a span
::: {lang=en}
followed by a native div (using this syntax since pandoc 2.0)
with another paragraph
:::
If you absolutely must set this info from the command-line, using a custom LaTeX template that contains, say, the following (if you're using Polyglossia/XeLaTeX):
\setotherlanguage[]{$myOtherLangs$}
and calling it with pandoc -V myOtherLangs="english,french" should solve your use-case.

pandoc places curly brackets around square brackets

I would like to use a LaTeX command in a markdown file. In the markdown file I add
\mycommand[options]{something}
when pandoc renders the markdown file to TeX it becomes
\mycommand{[}options{]}{something}
How can I prevent that from happening?
Works for me with pandoc 1.17.2:
$ echo '\mycommand[options]{something}' | pandoc -t latex
\mycommand[options]{something}

UTF-8 error in pandoc

I have a markdown + latex document that has compiled fine, until now when I added a special character ('รค') and get the error:
hGetContents: invalid argument (invalid UTF-8 byte sequence)
Following some advice I've found I have tried combinations of these in a custom preamble, but with no success:
\usepackage[utf8]{inputenc} % also tried [utf8x]
\usepackage[T1]{fontenc}
\usepackage[swedish]{babel}
Any suggestions on how to solve this?
The full preamble is:
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[a4paper,tmargin=2.5cm,bmargin=2.8cm,lmargin=3.5cm,rmargin=3.5cm]{geometry}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{fixltx2e}
\usepackage[all,error]{onlyamsmath}
\MakeRobust{\overrightarrow}
\usepackage{natbib}
\renewcommand{\normalsize}{\fontsize{11pt}{13.3pt}\selectfont}
pandoc call:
pandoc -f markdown file.mkd -H standard.tex --csl=amnat.csl --bibliography refs.bib -o output.pdf

Convert xypic matrix to eps. (LaTeX)

I have a lot of BIG xypic-matrices in my LaTeX file for one of my papers, and it takes long/infinite time to compile. Is there a way to convert just the separate xypic-pieces to eps files, that I include in my main document?
I suppose I use LaTeX to compile the xypic part, but then I will get an entire document, right?
(Can someone add the tag xypic to this question?)
Update: The solution I came up with was to have separate LaTeX documents for each xymatrix, and then use dvips -E to capture the figure. I created a script for Compiling + Create eps in Kile, the editor I use.
Use latex to compile, then a
dvips -E -o temp.eps temp.dvi
to make an eps out of it, then epscrop to make a small eps you can include later.
There are a few ways to run Latex on just a portion of a document; the most widely used is likely Emacs/Auctex's TeX-command-region (C-c C-r), which generates a temporary Latex file whose dvi/eps/pdf/whatever output is just that from the selected region.
You can use the preview package to only process your pictures. For example:
$ cat a.tex
\documentclass{article}
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{5pt}
\usepackage [arrow]{xy}
\begin{document}
\begin{preview}
\begin{xy}
(0,-20)="a", (0,0)="b"
\ar#{<.||} #<24mm> "a";"b"
\ar#^{<.||} #<16mm> "a";"b"
\ar#_{<.||} #<8mm> "a";"b"
\ar#0{<.||} "a";"b"
\ar#1{<.||} #<-8mm> "a";"b"
\ar#2{<.||} #<-16mm> "a";"b"
\ar#3{<.||} #<-24mm> "a";"b"
\end{xy}
\end{preview}
\end{document}
Then, when you run pdflatex, preview will generate a pdf with only the pictures in it. You can convert them to eps by ghostscript:
$ latex a
$ dvips -E -i -Pwww -o figure.000 a
or you can generate PNG images:
$ pdflatex a
$ gs -dNOPAUSE -r400 -dGraphicsAlphaBits=4 -dTextAlphaBits=4 \
-sDEVICE=png16m -sOutputFile=figure.png -dBATCH a.pdf
Then, you can include the generated files in your document.

Resources