To make a compact list in r-markdown - latex

From here I know that enumitem provides an option nosep which removes all spacing before and after the list as well as between the items, which works well in pure latex file test.tex as follows:
\documentclass[a4paper]{article}
\usepackage{lipsum}
\usepackage[shortlabels]{enumitem}
\setlist{nosep}
\begin{document}
\lipsum[11]
\begin{enumerate}
\item Each $T \in \mathcal{T}$ is either an even face or contains exactly 2 faces in its interior;
\item the interiors of distinct $T \in \mathcal{T}$ are disjoint.
\end{enumerate}
\lipsum[47]
\end{document}
I try to realize this nosep functionality in the rmd file test.rmd to make a compact file and then I try:
---
documentclass: article
classoption: a4paper
output:
pdf_document:
latex_engine: pdflatex
header-includes:
- \usepackage{lipsum}
- \usepackage[shortlabels]{enumitem}
- \setlist{nosep}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\lipsum[11]
\begin{enumerate}
\item Each $T \in \mathcal{T}$ is either an even face or contains exactly 2 faces in its interior;
\item the interiors of distinct $T \in \mathcal{T}$ are disjoint.
\end{enumerate}
\lipsum[47]
The spacing between the items is fine with the output PDF of test.rmd, but the spacing before and after the list keeps unchanged, which is very different from the output PDF of test.tex! Did I miss something?

The difference between your latex code and the rmarkdown code is that rmarkdown will automatically load all kinds of unnecessary packages, amongst others the parskip package, which will add some vertical space between paragraphs. You can remove this vertical space like this
---
documentclass: article
classoption: a4paper
output:
pdf_document:
latex_engine: pdflatex
header-includes:
- \usepackage{lipsum}
- \usepackage[shortlabels]{enumitem}
- \setlist{nosep}
- \setlength{\parskip}{0pt}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
\lipsum[11]
\begin{enumerate}
\item Each $T \in \mathcal{T}$ is either an even face or contains exactly 2 faces in its interior;
\item the interiors of distinct $T \in \mathcal{T}$ are disjoint.
\end{enumerate}
\lipsum[47]
(you might want to combine it with something like \setlength{\parindent}{1em} so paragraphs will again be recognisable)

Related

How can one start an \item with a math align environment?

In latex, I need to start an item in an enumerate list with an align* environment, but this environment starts with a new line, leaving an ugly empty space.
What I'm trying to achieve:
e^{ix}=cos x+i sin x
e^{i\pi} = -1
e^{i\pi}+1=0
What I'm trying:
\begin{enumerate}
\item \begin{align*}
e^{ix}&=\cos x+i\sin x\\
e^{i\pi}&=-1\\
e^{i\pi}+1&=0
\end{align*}
\end{enumerate}
Is there some other environment for this?
(I know that it is not recommended to start an item with a displayed math formula, but in this case I need to.)
As a workaround:
\documentclass{article}
\usepackage{mathtools}
\begin{document}
\begin{enumerate}
\item \hfill$\begin{aligned}[t]
e^{ix}&=\cos x+i\sin x\\
e^{i\pi}&=-1\\
e^{i\pi}+1&=0
\end{aligned}$\hfill\mbox{}
\end{enumerate}
\end{document}
A hack similar to the one in the accepted answer: the environment matrix needs no package to work and has no visible delimiters:
\documentclass{article}
\begin{document}
\begin{enumerate}
\item $\begin{matrix}
e^{ix}&=\cos x+i\sin x\\
e^{i\pi}&=-1\\
e^{i\pi}+1&=0
\end{matrix}$
\end{enumerate}
\end{document}
The output seems to me similar to the one you wanted to achieve:

Latex: fail to show line breaks in formula with ```\\```

I'm using rmarkdown to knit to pdf. However, line break \\ doesn't take effect. I suspect something is wrong with my package amsmath, because running the tex file also shows the same problem. But I don't know how to fix it.
(I installed R and Rstudio with tinytex, then installed tex live 2021. I don't know whether this can be the cause.)
The code is shown below:
---
title: "test"
date: '2022-04-07'
output:
word_document: default
pdf_document:
keep_tex: yes
---
$$
a = b \\
c = d
$$
Also, when I try knit to word, there are warning messages
[WARNING] Could not convert TeX math
a = b \
c = d
, rendering as TeX:
a = b \
^
unexpected control sequence \
expecting "%", "\label", "\tag", "\nonumber" or whitespace
Building the tex file, I still cannot see the expected linebreak in the output pdf. The tex file is shown below.
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\#ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
pdftitle={test},
hidelinks,
pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin#nat#width>\linewidth\linewidth\else\Gin#nat#width\fi}
\def\maxheight{\ifdim\Gin#nat#height>\textheight\textheight\else\Gin#nat#height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps#figure{htbp}
\makeatother
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\ifLuaTeX
\usepackage{selnolig} % disable illegal ligatures
\fi
\title{test}
\author{}
\date{\vspace{-2.5em}2022-04-07}
\begin{document}
\maketitle
\[
a = b \\
c = d
\]
\end{document}
I think this should perfectly answer your question:
https://tex.stackexchange.com/questions/46189/how-do-i-add-a-line-break-in-display-math-mode
In short: \[ \] is made for one equation/one line of math. To add a line-break, you should use an environment like gather or align. The code for align would look like this in your tex-code:
\begin{align}
a &= b \\
c &= d
\end{align}
If you don't need numbering of your equations, you would use the starred version of the respective environment (gather*, align*. Like this:
\begin{align*}
a &= b \\
c &= d
\end{align*}
If the link I provided does not fit your problem, please explain why. I am happy to correct this answer.

How it is possible to convert LaTeX document to Word (docx file) with all cross-references and labels for images and equations, tables by Pandoc?

I have a tex-file obtained originally from RStudio with bookdown. The original set of Rmd-files were deleted by accident.
So I need to convert below minimized correct tex-file to Word (docx file extension):
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\#ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
pdftitle={Simple document},
hidelinks,
pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[margin=1in]{geometry}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if#noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin#nat#width>\linewidth\linewidth\else\Gin#nat#width\fi}
\def\maxheight{\ifdim\Gin#nat#height>\textheight\textheight\else\Gin#nat#height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps#figure{htbp}
\makeatother
\usepackage[normalem]{ulem}
% Avoid problems with \sout in headers with hyperref
\pdfstringdefDisableCommands{\renewcommand{\sout}{}}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
\ifLuaTeX
\usepackage{selnolig} % disable illegal ligatures
\fi
\usepackage[]{biblatex}
\addbibresource{bibliography.bib}
\title{Simple document}
\author{}
\date{\vspace{-2.5em}}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}{Lemma}[section]
\newtheorem{corollary}{Corollary}[section]
\newtheorem{proposition}{Proposition}[section]
\newtheorem{conjecture}{Conjecture}[section]
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
\theoremstyle{definition}
\newtheorem{example}{Example}[section]
\theoremstyle{definition}
\newtheorem{exercise}{Exercise}[section]
\theoremstyle{remark}
\newtheorem*{remark}{Remark}
\newtheorem*{solution}{Solution}
\begin{document}
\maketitle
{
\setcounter{tocdepth}{2}
\tableofcontents
}
\hypertarget{format}{%
\section{Format}\label{format}}
Read \ref{format} for details.
Or read \protect\hyperlink{format}{format}.
Formatting:
\begin{itemize}
\tightlist
\item
sup-script or power - x\textsuperscript{2} ;
\item
sub-script - y\textsubscript{3} ;
\item
strike through - \sout{strike through} ;
\item
term -- definition ;
\item
long term --- long definition .
\end{itemize}
Here comes \textbf{footnote} \footnote{Footnote text} with its contents.
Image with caption, see \ref{fig:image}.
\begin{figure}
\centering
\includegraphics{image.png}
\caption{\label{fig:image} Image}
\end{figure}
Labeled table is below, see table \ref{tab:table}.
\begin{longtable}[]{#{}ll#{}}
\caption{\label{tab:table}\\
My table}\tabularnewline
\toprule
\textbf{Col 1 header} & \textbf{Col 2 header} \\
\midrule
\endfirsthead
\endhead
r1c1 & r1c2 \\
r2c1 & r2c2 \\
\bottomrule
\end{longtable}
Inline: \(a^2 + b^2 = c^2\).
Block:
\[ E = \frac{mc^2}{\sqrt{1-\frac{v^2}{c^2}}} \]
Labeled, see \eqref{eq:eq}:
\begin{equation}
f\left(k\right) =\binom{n}{k} p^k\left(1-p\right)^{n k}
\label{eq:eq}
\end{equation}
See LaTeX \autocite{latex} site for details.
Code is below, see \ref{exm:code}:
\begin{example}
\protect\hypertarget{exm:code}{}{\label{exm:code} }Hello world
\end{example}
\begin{verbatim}
int main(){
return 0;
}
\end{verbatim}
\hypertarget{references}{%
\section*{References}\label{references}}
\addcontentsline{toc}{section}{References}
\printbibliography
\end{document}
When I try to convert it to docx using pandoc (2.16.1 version) command below:
pandoc simple.tex --to docx+native_numbering --output simple.docx --table-of-contents --toc-depth 5 --number-sections --citeproc --verbose --csl ieee.csl
(In the above command ieee.csl was downloaded from Zotero).
I get
and I see many strange things:
[fig:image] along with Figure with number;
[tab:table] along with Table with number;
[eq:eq] instead of equation number;
[exm:code] instead of example number.
Is it possible to replace (or suppress them for Figure and Table) above strange […] fields by correct numbers and links using Pandoc to get correct docx output?
This is currently not supported by plain pandoc, but you can use filters to get the numbering back. E.g., the resolve-references.lua filter in the OpenJournals "Inara" project does something comparable. You may have to adjust and extend it to fully suit your requirements.

In latex, the integral symbol '\int' displays as '\Delta' in the compiled pdf, why?

This happens in some (but not all) documents, where '$\int$' in an equation displays as '$\Delta$' in the compiled pdf. For those documents, I tried both Winedt and Overleaf and the same things just happen.
The problem can be replicated by using the following codes in the main.tex file:
\documentclass[a4paper,10pt]{autart}
\pagestyle{plain}
\date{\today}
\usepackage{esint}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\setlength {\marginparwidth }{2cm}
\begin{document}
\begin{frontmatter}
\title{Generalized $t$-Distribution Noise Model\thanksref{footnoteinfo}}
\thanks[footnoteinfo]{Acknowledgement...}
\author[AuthorCategory1]{Author 1}\ead{author1#somewhere}
\author[AuthorCategory2]{Author 2}\ead{author2#somewhere}
\address[AuthorCategory1]{Address 1}
\address[AuthorCategory2]{Address 2}
\begin{abstract}
---
\end{abstract}
\end{frontmatter}
\section{This is a section}
\begin{eqnarray}
\int \nonumber
\end{eqnarray}
\end{document}
The autart.cls can be found online, for example at https://hal.archives-ouvertes.fr/file/index/docid/514508/filename/autart.cls
You will get a result like this:
And from my own exploration, the problem can be solved surprisingly if changing the title from
\title{Generalized $t$-Distribution Noise Model\thanksref{footnoteinfo}}
to
\title{Title\thanksref{footnoteinfo}}
And you will get the correct integral symbol as follows:
It seems that you can also solve the problem by removing the "esint" package, or change the document class from autart to article. I guess there is some conflicts defined in these class/packages, maybe, and result in this funny phenomenon.
Is there a way to solve the problem without changing the title, while using autart document class and esint package?
Thanks.
Ironically, the problem is in the \no#harm macro your class defines, which redefines \protect. You can work around the problem by removing this defintion like this:
\documentclass{autart}
\pagestyle{plain}
\date{\today}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{xpatch}
\makeatletter
\xpatchcmd{\no#harm}{\def\protect{\noexpand\protect\noexpand}}{}{\typeout{patch ok}}{\typeout{patch fail}}
\makeatother
\usepackage{esint}
\setlength {\marginparwidth }{2cm}
\begin{document}
\begin{frontmatter}
\title{Generalized $t$-Distribution Noise Model\thanksref{footnoteinfo}}
\thanks[footnoteinfo]{Acknowledgement...}
\author[AuthorCategory1]{Author 1}\ead{author1#somewhere}
\author[AuthorCategory2]{Author 2}\ead{author2#somewhere}
\address[AuthorCategory1]{Address 1}
\address[AuthorCategory2]{Address 2}
\begin{abstract}
---
\end{abstract}
\end{frontmatter}
\section{This is a section}
\begin{eqnarray}
\int \nonumber
\end{eqnarray}
\end{document}

Latex: Multiple Table of Contents

What I am looking for is the following table of contents structure:
Table of Contents
1 Vorwort
2 Hallo
3 Und so weiter
Document with these Chapters, but at the beginning of each chapter its specific table of contents:
1 Vorwort
----- here subtoc -----
1.1 subtoc
1.2 Test
---- here the beginning of section test ----
1.2 Test
The problem however is, that in these different chapters all subtocs are combined, such as in my example:
\documentclass{scrbook}
\usepackage[latin1]{inputenc}
\usepackage[ngerman]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{blindtext}% generiert Beispieltext
\usepackage{tocstyle}[2008/10/20]% experimentelles KOMA-Script-Paket
\usetocstyle{standard}
\newcounter{normaltocdepth}
\newcommand*{\startstory}[1]{%
\newpage\chapter{#1}\addtocontents{toc}{\protect\startstory}%
}
\newcommand*{\afterstory}{%
\addtocontents{toc}{\protect\afterstory}
}
\newcommand*{\maintoc}{%
\begingroup
\setcounter{normaltocdepth}{\value{tocdepth}}%
\renewcommand*{\startstory}{%
\setcounter{tocdepth}{1}%
}%
\renewcommand*{\afterstory}{%
\setcounter{tocdepth}{\value{normaltocdepth}}%
}%
\tableofcontents
\endgroup
}
\newcommand*{\storytoc}{%
\begingroup
\setcounter{normaltocdepth}{\value{tocdepth}}%
\setcounter{tocdepth}{-2}%
\renewcommand*{\startstory}{%
\setcounter{tocdepth}{\value{normaltocdepth}}%
}%
\renewcommand*{\afterstory}{%
\setcounter{tocdepth}{-2}%
}%
\showtoc{toc}
\setcounter{tocdepth}{\value{normaltocdepth}}% Hinzugefügt!
\endgroup
}
\newcommand{\neuestory}[3]{
\startstory{#3}
\vspace{10mm}
\section*{Abstract}
\small
\noindent #1
\section*{Personen}
\noindent #2
\normalsize
\vspace{10mm}
\section*{Inhalt}
\storytoc
%\cleardoublepage
}
\begin{document}
\maintoc
\newpage
\chapter{Vorwort}
blabla
\neuestory{ABC}{eine Frau}{Hallo}
\section{Test}
abc
\afterstory
\neuestory{DEF}{eine Mann}{Und so weiter}
\section{weiterheiter}
def
\afterstory
\end{document}
The answer is the minitoc package.
Works fine!

Resources