Setting otherlangs from the command line using pandoc - latex

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.

Related

Compile two versions of a document from the same latex source

How to automatically compile two versions of the same document, for example version without answers for the students and another version with answers for myself?
I have a small bash script to do a dual format.
function latex-ans () {
n=$(basename $1 .tex) # strip .tex in filename foo.tex -> foo
ln -s $n.tex $n-ans.tex # create a soft link (for instance foo-ans.tex -> foo.tex)
pdflatex '\def\withanswer{1} \input{'$n-ans'}' && pdflatex $n
% first format the version with answers and then the normal version
rm $n-ans.tex $n-ans.log
% remove useless files
}
If I have a file foo.tex, this commands formats both versions of the file and generates two pdf: foo.pdf and foo-ans.pdf. Thanks to the renaming of foo.tex through the ln -s, it also keeps separate foo.aux and foo-ans.aux to preserve useful information on both versions.
At the latex level, I basically do the same and use the macro \withanswers to configure my packages.
There are several packages that allow to conditionally exclude certain parts of the document, for example the exercise package.
With TeXstudio, the following magic comment can be used to automatically compile both versions at once (including repeated compilation for cross-references, bibliographies, indices etc.):
% !TeX program = latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=% -pretex="\newcommand{\version}{noanswer}" -usepretex % | latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=%_solution -pretex="\newcommand{\version}{}" -usepretex % | txs:///view-pdf "?am)_solution.pdf"
\documentclass{article}
% setting a default value in case it is compiled without the magic comment
\ifdefined\version
\else
\def\version{noanswer}
\fi
\usepackage[\version]{exercise}
\begin{document}
\begin{Exercise}[title={Title},label=ex1]
question text
\end{Exercise}
\begin{Answer}[ref={ex1}]
solution
\end{Answer}
\end{document}

How to make sphinx document via pdflatex \raggedright?

How can I render reST document with \raggedright command applied to all the text if I use pdflatex?
Use a custom template.
In the following example, I'll be using xelatex. I would copy the default template for xelatex from /usr/local/lib/python2.7/site-packages/docutils/writers/latex2e/xelatex.tex to a file called mytemplate.tex
The default template looks like this:
$head_prefix% generated by Docutils <http://docutils.sourceforge.net/>
% rubber: set program xelatex
\usepackage{fixltx2e}
\usepackage{fontspec}
% \defaultfontfeatures{Scale=MatchLowercase}
$requirements
%%% Custom LaTeX preamble
$latex_preamble
%%% User specified packages and stylesheets
$stylesheet
%%% Fallback definitions for Docutils-specific commands
$fallbacks$pdfsetup
$titledata
%%% Body
\begin{document}
$body_pre_docinfo$docinfo$dedication$abstract$body
\end{document}
You could add \raggedright just after \begin{document}.
For rendering the document, I'd use the following commands:
rst2xetex --template=mytemplate.tex --use-latex-docinfo mydoc.rst
xelatex mydoc.tex
xelatex mydoc.tex
rm -f *.out *.aux *.log mydoc.tex
(This is on a UNIX-like operating system.)

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}

How to set the font size for code blocks in pandoc markdown?

this small example:
An example code snippet:
~~~{.cpp}
class A
{
public:
static void f1 () {};
virtual void f2 () = override;
};
~~~
can be used to generate a PDF output with:
pandoc -o block-code.pdf block-code.txt
resulting in
The font sizes of both the code and the text that are equal. How can I change the font size of the code snippets for the pdf (LaTex) pandoc output?
You can simply add \small before the beginning of the code snippet and \normalsize after (to return to normal).
You can also add other similar commands. For instance, if your document is doublespaced, you can add \singlespace before the code snippet and \doublespacing after.
For this to work you need to add in the yaml at the beginning of your document the following:
---
header-includes:
- \usepackage{setspace}
---
I solved this problem for me by writing several LaTeX snippets into extra files I keep around:
cat make-code-footnotesize.tex
\renewenvironment{Shaded} {\begin{snugshade}\footnotesize} {\end{snugshade}}
I have such snippets for all different sizes: huge, LARGE, Large, large, normalsize, small, footnotesize, scriptsize, tiny.
To apply them when running pandoc, just include the respective LaTeX snippet with the -H parameter:
pandoc -o block-code.pdf block-code.txt \
-H make-code-scriptsize.tex --highlight-style=espresso
Result:
Note, this controls the font sizes for all code blocks in the PDF. It does not allow you to vary sizes from block to block. Of course, it also doesn't work for HTML, ODT, EPUB or other output -- only for LaTeX and PDF output.
I've developed a filter for pandoc https://github.com/chdemko/pandoc-latex-fontsize for this purpose:
Install this filter with pip:
$ pip install pandoc-latex-fontsize
add for example
---
pandoc-latex-fontsize:
- classes: [c, listing]
size: footnotesize
---
to your metadata block and specify the listings you want to be of size footnotesize:
~~~{.c .listing}
int main(void) {
return 0;
}
~~~
then run pandoc with
$ pandoc --filter pandoc-latex-fontsize ...
On pandoc 1.16.0.2 I did get the same problem but wasn't solved by previous answers.
When using default code highlighting of this version and exporting it with beamer (-t beamer) I got following generated output (for outputting a jmeter command):
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{./jmeter.sh} \NormalTok{-q prod.properties -p jmeter.properties -n -t mytest.jmx -l mylog.log}
\end{Highlighting}
\end{Shaded}
By searching directly in the pandoc code with grep -r "Highlighting" * I found following code:
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
And I replaced it with the following to have a tiny font size in my custom pandoc template (see pandoc -D beamer):
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},fontsize=\tiny}
Now I have a tinier font size by running the following command:
pandoc -t beamer input.md -o output.pdf --template beamer.template
I use pandoc with the --listings option and a custom eisvogel.latex template.
Download the original eisvogel-template to a new file eisvogel_custom.latex
Open the file, find the line basicstyle = \color{listing-text-color}\small\ttfamily{}\linespread{1.15},
Change the \small to \footnotesize, or \tiny
Save the file
Now run pandoc with the following options:
pandoc --pdf-engine=xelatex --template=eisvogel_custom --listings -o block-code.pdf block-code.txt
To disable the line-numbering, add the following lines at the top of block-code.txt
---
listings-disable-line-numbers: true
...
All options can be found here: https://github.com/Wandmalfarbe/pandoc-latex-template#custom-template-variables
Tested with pandoc 2.6 using docker-image dalibo/pandocker:stable

Set the language of a listings environment in pandoc

I am struggling to convince pandoc to set the language for a listing automatically when converting from extended markdown (pandoc) to latex with the --listings argument. This is the file foo.txt:
A listing follows.
~~~{.prolog}
% fooing around
foo :-
format("bar~n").
~~~~
When I use pandoc -s foo.txt -o foo.html, the code is highlighted.
When I use pandoc -s foo.txt -o foo.latex, the code is highlighted, using the custom commands inserted by pandoc in the LaTeX preamble.
When I use pandoc -s foo.txt --listings foo.latex, the code is not highlighted, as the language option to the listings environment is not set. I can work around this:
~~~{.prolog language=Prolog}
but this defeats the purpose. Am I missing something or is this functionality not supported? If it omitted on purpose, what is the reason?

Resources