jay.sf
jay.sf

Reputation: 73592

Header line break in Rmarkdown, change textsize after, included as a whole in TOC

I want a header line break in Rmarkdown, after the break the text should appear smaller (it's the subtitle). The guy should appear in the table of contents as a whole (rather with author's name). Here's my attempt where only the header appears in the TOC:

---
title: "Untitled"
output:
  pdf_document:
    toc: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# This is the header
\vspace{-4mm}

**This should be the subtitle after the line-break**

\vspace{2mm}

*Author's Name*

\vspace{10mm}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Desired outcome something like this: enter image description here

Upvotes: 1

Views: 2161

Answers (2)

www
www

Reputation: 4224

I think LaTeX might give you more options for what you'd like here. In LaTeX, you can you "\\" to create a line break almost anywhere in the code. Then, to get the TOC left-aligned like in your example output, you can use the "tocloft" package and set the indentation for subsections with "\cftsetindents{subsection}{0in}{0in}".

---
title: "Untitled"
output: pdf_document
header-includes:
  - \usepackage{tocloft}
---
\cftsetindents{subsection}{0in}{0in}

\hypertarget{toc}{}
\thispagestyle{plain}
\tableofcontents

\section{Header}

\subsection[Subtitle \\ \emph{Author's Name}]{Subtitle \\\\ \normalfont{\emph{Author's Name}}}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod 
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim 
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea 
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate 
velit esse cillum dolore eu fugiat nulla pariatur.

Output:

enter image description here

Edit:

If you want to remove the page number from being added to the "\section" names in the TOC (i.e. "Header" in this case), then there's a two-step solution I know of: (1) you can add an asterisk (*) between "\section" and the name of the section "{Header}" to exclude that section from being listed in the TOC; (2) then you can add the bold-font name of the section into the related subsection portion of the TOC (again using \\ for line breaks) so that the section name appears in the TOC by name only, not number.

Output:

enter image description here

Upvotes: 1

mb21
mb21

Reputation: 39488

The closes you're going to get what you want with markdown will be to use subtitles and sub-subtitles (they map to h2 or h3 in html respecitvely).

# This is the header

## This should be the subtitle after the line-break

### *Author's Name*

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

With the default template you get:

enter image description here

If you want more control over the layout, you should tweak your pandoc LaTeX template.

Upvotes: 0

Related Questions