How to make sphinx document via pdflatex \raggedright? - latex

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.)

Related

Converting from latex to markdown with pandoc, images not converted

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

Same source compiled for customer and developers

I have a document in Latex with change logs of my software
I would to create 2 output documents from 1 source:
for the customers, include only short descriptions;
for the developers, include more details;
I saw in Compile two versions of a document from the same latex source the use of package exercise but there is something more generics?
For example an environment /role1 and /role2 ?
In the linked thread I use the exercise package just as an example. You can use the same mechanism for any document you want.
For example with texstudio:
% !TeX program = latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=% -pretex="\newif\ifnotes \notesfalse" -usepretex % | latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=%_notes -pretex="\newif\ifnotes \notestrue" -usepretex % | txs:///view-pdf "?am)_notes.pdf"
\documentclass{article}
\begin{document}
text for everyone
\ifnotes
notes visible only for devs
\else
short description for customers
\fi
\end{document}
or to be compiled manually with:
latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=<filename> -pretex="\newif\ifnotes \notesfalse" -usepretex <filename>
latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=<filename>_notes -pretex="\newif\ifnotes \notestrue" -usepretex <filename>
(replace <filename> with the base name of your .tex file)
As an alternative approach you could also use this clever trick: https://topanswers.xyz/tex?q=583#a1204

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}

\newcommand with arguments in Kritr not working

I am quite new with Knitr, and I am trying to write my first document "importing" some "\newcommand" from my latex files.
All my "\newcommand" are processed perfectly with the exception of those with "arguments".
For instance I have added the packages I normally use in latex:
header-includes:
-\usepackage{a4wide,amsmath,amsthm,epsfig,graphicx}
- \usepackage{amsmath,amsthm,amssymb}
- \usepackage{amsfonts}
- \usepackage{graphics}
- \usepackage{subfigure}
- \usepackage{dsfont}
- \usepackage{bbm}
- \usepackage{bm}
- \usepackage{xcolor}
- \usepackage{dcolumn}
- \usepackage{pstricks,fancyhdr,fancybox}
- \usepackage{dcolumn}
- \usepackage{bm}
But the following does not work
\newcommand{\PR}[1]{\bm{P}\left\{{#1}\right\}}
The error is
output file: newfile.knit.md
"../Documents/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS newfile.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output newfile.pdf --template "C:\Users\P14814\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"
! Missing $ inserted.
<inserted text>
$
l.180 \newcommand{\PR}{[}1{]}\{\bm{P}
pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"../Documents/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS newfile.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output newfile.pdf --template "C:\Users\P14814\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43
Execution halted
Your help is very much appreciated!!
I strongly recommend you to put these commands in an external file, say, preamble.tex, and include it via the includes option.
output:
pdf_document:
includes:
in_header: preamble.tex
See Section 3.3.7.3 of the R Markdown book: https://bookdown.org/yihui/rmarkdown/pdf-document.html
Using header-includes is not entirely safe. Pandoc may mangle certain characters when generating the LaTeX output. If your Pandoc version is higher than 2.0 (check rmarkdown::pandoc_version()), you may use a raw LaTeX block to protect the LaTeX content, e.g.,
header-includes: |
```{=latex}
\usepackage{a4wide,amsmath,amsthm,epsfig,graphicx}
\usepackage{amsmath,amsthm,amssymb}
......
```

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.

Resources