Indent without adding a bullet point or number in RMarkdown - r

I want to make an indented list, but I don't want it to have bullet points or numbers. I am using Rmarkdown in RStudio, and knitting to html.
#### bla bla bla
* Example indented line with bullet point
* Another indent with another bullet point
* Yea this is good except for the stupid bullets!
1. Example indented line with numbers
* sure and an indent with a bullet too
2. But there's these stupid numbers now!
two spaces doesn't indent at all
or nest indent with 4
yea still no indent with 2.
four spaces ALSO doesn't indent
just makes some stupid code
why do you hate indents rmd??

This can be achieved using Line Blocks which are built-in to the R Markdown syntax. If you want the indentation to be respected, you can start a line with |.
This approach works across multiple output formats and doesn't require any additional CSS styling:
---
output:
html_document: default
pdf_document: default
---
Here is some text with no indentation
| A list
| A sublist
| Sublist Item 2
| Sublist Item 3

If you want to change how a list looks and you're outputting to HTML, use css:
---
title: "ListTest"
output: html_document
---
<style>
.nobullet li {
list-style-type: none;
}
</style>
<div class="nobullet">
* This list
* Doesn't have bullets
</div>
* This list
* Is normal
This won't work for other output formats.

Related

Control line spacing in unordered list in R Markdown

I am creating a html report using R Markdown. The first paragraph contains a list of categories represented by certain samples with short descriptions. Here is an example:
This is the test list:
* **first group**:
+ **item 1** - this is the description of an item
* **second group**:
+ **item 2** - this is the description of an item
+ **item 3** - this is the description of an item
* **third group**:
+ **item 4** - this is the description of an item
* **fourth group**:
+ **item 5** - this is the description of an item
+ **item 6** - this is the description of an item
The text was inserted outside the code chunks. I would like to preserve this (even) spacing between the lines in knitted document, so it would be easy readable. Otherwise it looks too squished. Unfortunately R Markdown changes spacing, so some of them are uneven. I tried using trailing spaces and defining line breaks with br, but nothing changed. I could off course edit the html file manually after knitting, but this is a brute force way. R Markdown has to have some option to deal with it - but I was not able to figure it out myself. Would appreciate any help.
You could add a css tag at the start of your rmarkdown document to specify how you would like the html output to interpret line heights:
```{css}
li {
line-height: 3;
}
...

How to add a line in the pdf generated pdf by `exams` using the `exams2nops`?

We are generating a pdf through exams2nops using the items in blocks of choice, we would like to delimitate the blocks in the PDF adding a horizontal line after the last exercise of each block. Having that in mind we added a ***, ---, <hr/> however the behavior was always the same:
I would like a single line without adding the exercise number that's next in the exam:
It is not so easy to solve this by putting the horizontal line into the exercise file. The reason is that the line is needed after the answerlist but the answerlist is not formatted in the exercise but by exams2nops.
A workaround is to tweak the definition of the {question} environment in the LaTeX template used by exams2nops. By default this is simply:
\newenvironment{question}{\item}{}
Where \item is executed at the beginning of the {question} and nothing at the end of it. Changing this by
\renewenvironment{question}{\item}{\hrulefill}
would insert a horizontal line after every question. If you just want it after selected questions you need to insert if/else statements for certain enumerated items. For example, for inserting the horizontal rule after the second item only, you can redefine:
\renewenvironment{question}{\item}{\ifnum\value{enumi}=2 {\hrulefill} \else {} \fi}
Thus, you get the enumi counter from the {enumerate} environment that you use and compare it with 2. If true, you insert the horizontal line, and otherwise you do nothing.
Adding escapes for the backslashes you can pass this re-definition to exams2nops through the header argument:
exams2nops(c("swisscapital", "switzerland", "tstat2", "deriv2"),
header = "\\renewenvironment{question}{\\item}{\\ifnum\\value{enumi}=2 {\\hrulefill} \\else {} \\fi}")
The resulting output is:

R Markdown Bullet List with Multiple Levels

https://github.com/rstudio/cheatsheets/raw/master/rmarkdown-2.0.pdf
The cheat sheet above lists the following syntax to generate a bulleted list in R Markdown. * is the primary solid bullet. + is a secondary hollow bullet. And - is a tertiary solid square.
* unordered list
+ sub-item 1
+ sub-item 2
- sub-sub-item 1
After I render the output with knitr I don't get the expected output. I get what's shown below. The second and third lines are not indented. Only the very last line is indented, and only one indent instead of the expected two. And all bullets are the secondary hollow style. It's as if I put the following syntax into R Studio, but I didn't.
+ unordered list
+ sub-item 1
+ sub-item 2
+ sub-sub-item 1
How do I get what I intended, the first chunk of pasted syntax?
For each sub-level instead of one tab, include two:
* unordered list
+ sub-item 1
+ sub-item 2
- sub-sub-item 1
Output:
See the Pandoc documentation under Block content in list items.
Was looking for the same, you may do also something like
<body>
<h3>Today's shopping list:</h3>
<ul>
<li>Milk</li>
<li>Eggs</li>
<li>Cereal</li>
<li>Fruit</li>
</ul>
</body>
This link may help too as well.

Scrape all child paragraphs under heading (preferable rvest)

My objective is to use the library(tm) toolkit on a pretty big word document. The word document has sensible typography, so we have h1 for the main sections, some h2and h3 subheadings. I want to compare and text mine each section (the text below each h1 - the subheadings is of little importance - so they can be included or excluded.)
My strategy is to export the worddocument to html and then use the rvestpacakge to extract the paragraphs.
library(rvest)
# the file has latin-1 chars
#Sys.setlocale(category="LC_ALL", locale="da_DK.UTF-8")
# small example html file
file <- rvest::html("https://83ae1009d5b31624828197160f04b932625a6af5.googledrive.com/host/0B9YtZi1ZH4VlaVVCTGlwV3ZqcWM/tidy.html", encoding = 'utf-8')
nodes <- file %>%
rvest::html_nodes("h1>p") %>%
rvest::html_text()
I can extract all the <p>with html_nodes("p"), but thats just one big soup. I need to analize each h1 separately.
The best would probably be a list, with a vector of p tags for each h1 heading. And maybe a loop with somehting like for (i in 1:length(html_nodes(fil, "h1"))) (html_children(html_nodes(fil, "h1")[i])) (which is not working).
Bonus if there is a way to tidy words html from within rvest
Note that > is the child combinator; the selector that you currently have looks for p elements that are children of an h1, which doesn't make sense in HTML and so returns nothing.
If you inspect the generated markup, at least in the example document that you've provided, you'll notice that every h1 element (as well as the heading for the table of contents, which is marked up as a p instead) has an associated parent div:
<body lang="EN-US">
<div class="WordSection1">
<p class="MsoTocHeading"><span lang="DA" class='c1'>Indholdsfortegnelse</span></p>
...
</div><span lang="DA" class='c5'><br clear="all" class='c4'></span>
<div class="WordSection2">
<h1><a name="_Toc285441761"><span lang="DA">Interview med Jakob skoleleder på
a_skolen</span></a></h1>
...
</div><span lang="DA" class='c5'><br clear="all" class='c4'></span>
<div class="WordSection3">
<h1><a name="_Toc285441762"><span lang="DA">Interviewet med Andreas skoleleder på
b_skolen</span></a></h1>
...
</div>
</body>
All of the p elements in each section denoted by an h1 are found in its respective parent div. With this in mind, you could simply select p elements that are siblings of each h1. However, since rvest doesn't currently have a way to select siblings from a context node (html_nodes() only supports looking at a node's subtree, i.e. its descendants), you will need to do this another way.
Assuming HTML Tidy creates a structure where every h1 is in a div that is directly within body, you can grab every div except the table of contents using the following selector:
sections <- html_nodes(file, "body > div ~ div")
In your example document, this should result in div.WordSection2 and div.WordSection3. The table of contents is represented by div.WordSection1, and that is excluded from the selection.
Then extract the paragraphs from each div:
for (section in sections) {
paras <- html_nodes(section, "p")
# Do stuff with paragraphs in each section...
print(length(paras))
}
# [1] 9
# [1] 8
As you can see, length(paras) corresponds to the number of p elements in each div. Note that some of them contain nothing but an which may be troublesome depending on your needs. I'll leave dealing with those outliers as an exercise to the reader.
Unfortunately, no bonus points for me as rvest does not provide its own HTML Tidy functionality. You will need to process your Word documents separately.

Multiple lines of text in single cell of simple table?

I found this question, but I don't want explicit <br>s in my cell; I just want it to line-wrap where necessary.
e.g.,
================ ============
a short sentence second cell
a much longer bottom right
sentence
================ ============
I want "a much longer sentence" to all fit in one cell. I'd need to use very long lines of text unless I can find a way to wrap it. Is this possible?
I'm using NoTex w/ PDF output if relevant.
There is a clean way. The issue is by default the columns are set to no-wrap, so that's why you get the scroll. To fix that you have to override the css with the following:
/* override table no-wrap */
.wy-table-responsive table td, .wy-table-responsive table th {
white-space: normal;
}
The simple table style does not support wrapping blocks. Use the grid style instead, like this:
+------------------+--------------+
| a short sentence | second cell |
+------------------+--------------+
| a much longer | bottom right |
| sentence | |
+------------------+--------------+
These tables are more tedious to work with, but they're more flexible. See the full documentation for details.
A workaround for this problem is to use a replace directive:
================ ============
a short sentence second cell
|long_sentence| bottom right
================ ============
.. |long_sentence| replace:: a much longer sentence
The example ddbeck presented may work because the sentence is to short. In the case of the lenght of the sentence dont fit in the screen, the sentence will not continue in a new line. Instead, the table will create a horizontal scrollbar. There is no clean way for solving this problem. You can implicit use pipe to implicitly change line like you saw here.
If you want alternatives to write your tables in restructuredtext, more pratical ways, you can check it in Sphinx/Rest Memo.
I wrote a python utility to format fixed-width plaintext table with multiline cells: https://github.com/kkew3/tabulate. Hope it helps.

Resources