1

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}
Ivan
  • 13
  • 4

1 Answers1

2

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.

Dugnom
  • 342
  • 1
  • 5
  • 12
  • Thanks a lot! It works now. But I'm still a little confused as I was able to use '\\' to make a line break in the formular on other devices. – Ivan Apr 07 '22 at 08:34
  • To be honest, I don't know how R and Rstudio works. Maybe it generated different LaTeX-code on the other devices for some reason. But that is pure speculation. I will make a small edit to my answer, because I interpreted the $-signs in your first codeblock as LaTeX-code and don't want to spread something I know nothing about. – Dugnom Apr 07 '22 at 08:57
  • Thank you very much! I can simply adapt my code in Rmarkdown to make it work. – Ivan Apr 08 '22 at 00:21