"runin" & bfseries style for rmarkdown html section and subsection title - html

I want to use the "runin" & bfseries style for section and subsection titles using rmarkdown
in both pdf and html, where "runin" means the section or subsection titles and the text are on the same line, and bfseries means using the bold black font-style for the titles.
In pdf, we can get these two goals by resorting to the latex and \usepackage{titlesec}, while in html I have no idea for the issues especially for the "runin" one (the bfseries one seems that it can be meet by manually adding something like **sec-title** or **subsec-title** ).
My demo rmd file is given as follows:
---
output:
html_document:
toc: no
keep_md: no
pdf_document:
latex_engine: pdflatex
keep_tex: no
header-includes:
- \usepackage{lipsum}
- \usepackage{titlesec}
- \titleformat{\section}[runin]{\bfseries}{}{0em}{}
- \titlespacing{\section}{0pt}{*3}{2ex}
- \titleformat{\subsection}[runin]{\bfseries}{}{0em}{}
- \titlespacing{\subsection}{0pt}{*1}{2ex}
---
```{css style, echo = FALSE}
h1{font-size: 20px; color: black;}
h2{font-size: 20px; color: black;}
body{font-size: 20px;}
```
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Ex. 14.3
In Section 14.2.6 we discuss the use of CART or PRIM for constructing generalized association rules. Show that a problem occurs with either of these methods when we generate the random data from the productmarginal distribution; i.e., by randomly permuting the values for each of the variables. Propose ways to overcome this problem.
## Solution
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
\lipsum[1]
# Ex. 14.4
Cluster the demographic data of Table $14.1$ using a classification tree. Specifically, generate a reference sample of the same size of the training set, by randomly permuting the values within each feature. Build a classification tree to the training sample (class 1 ) and the reference sample (class 0 ) and describe the terminal nodes having highest estimated class 1 probability. Compare the results to the PRIM results near Table $14.1$ and also to the results of $K$-means clustering applied to the same data.
## Solution
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
\lipsum[2]

This can be done with the help of CSS. Add the class .inline with title and subtitle for which you want to use "runin" & bfseries style.
---
output:
html_document:
toc: no
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{css style, echo = FALSE}
h1{font-size: 20px; color: black;}
h2{font-size: 20px; color: black;}
body{font-size: 20px;}
div.inline > h1,
div.inline > h2,
div.inline > p {
display: inline;
}
div.inline > h1,
div.inline > h2 {
font-weight: bold;
}
div.inline {
margin-top: 1em;
margin-bottom: 1em;
}
```
# Ex. 14.3 {.inline}
In Section 14.2.6 we discuss the use of CART or PRIM for constructing generalized association rules. Show that a problem occurs with either of these methods when we generate the random data from the productmarginal distribution; i.e., by randomly permuting the values for each of the variables. Propose ways to overcome this problem.
## Solution {.inline}
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
# Ex. 14.4 {.inline}
Cluster the demographic data of Table $14.1$ using a classification tree. Specifically, generate a reference sample of the same size of the training set, by randomly permuting the values within each feature. Build a classification tree to the training sample (class 1 ) and the reference sample (class 0 ) and describe the terminal nodes having highest estimated class 1 probability. Compare the results to the PRIM results near Table $14.1$ and also to the results of $K$-means clustering applied to the same data.
## Solution {.inline}
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla

If it is also a possibility to switch to Quarto (.qmd), there is the reference-able exercise option, e.g.
---
title: Exercise Example
format:
html:
css: styles.css
---
::: {#exr-14.3}
In Section 14.2.6 we discuss the use of CART or PRIM for constructing generalized association rules. Show that a problem occurs with either of these methods when we generate the random data from the productmarginal distribution; i.e., by randomly permuting the values for each of the variables. Propose ways to overcome this problem.
:::
::: {.solution}
bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
:::
where styles.css looks like this:
.proof-title {
font-weight: bold;
}
Result:

Related

How do I multiply values within id and display answer?

How do multiply each value in the div with class="crtTotal" and have a div output that displays the answer: for example 1.75 x 3.65 x 2.10 = 13.41
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="box" class="boxlit">
<div class="box" data-id="75">Weston Bears - Cooks Hill United
<br>Home
<div class="crtTotal">1.75</div>
</div>
<div class="box" data-id="79">Logan Lightning - Brisbane City
<br>Draw
<div class="crtTotal">3.65</div>
</div>
<div class="box" data-id="81">Eastern Suburbs Brisbane - Moreton Bay United Jets
<br>Home
<div class="crtTotal">2.10</div>
</div>
<br>
<div class="total">Total</div>
Here Is the script I Came up with but works on sum (Addition), how do I Get It To Multiply for example 1.75 x 3.65 x 2.10 = 13.41
<script>
$(function() {
var sum = 0;
$('.crtTotal').each(function(){
sum += parseFloat(this.innerHTML, 10)
})
$('.total').text(sum);
})
</script>
The issue is because you initialise sum to zero. Therefore every number you multiply by sum is still zero. To fix this you could create an array of all values then use reduce() to multiply them all together.
To display the output to 2DP you can use toFixed(2) - but be aware this will convert the value to a string. Only call this function when presenting the value in the UI, not when it will be used for further calculation.
let values = $('.crtTotal').map((i, el) => parseFloat(el.textContent)).get();
let total = values.reduce((a, b) => a * b);
$('.total').text(total.toFixed(2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div id="box" class="boxlit">
<div class="box" data-id="75">Weston Bears - Cooks Hill United
<br>Home
<div class="crtTotal">1.75</div>
</div>
<div class="box" data-id="79">Logan Lightning - Brisbane City
<br>Draw
<div class="crtTotal">3.65</div>
</div>
<div class="box" data-id="81">Eastern Suburbs Brisbane - Moreton Bay United Jets
<br>Home
<div class="crtTotal">2.10</div>
</div>
<br>
<div class="total">Total</div>
</div>

HTML5: Emmet works wrong if I use a smile in my text

Windows 10, Sublime Text Editor 3, Emmet.
My html file has UTF-8 encoding. I want to insert a smile in the text (I inserted the smile by Win + . keys pressing):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
p{A smile 😜}
</body>
</html>
After I wrote p{A smile 😜} text I pressed Tab key but the string wasn't transformed to html code. Hm... I pressed Tab key again and I got the expected result.
Ok... But now I will get the problem in Emmet. I added the text (ol):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>A smile 😜</p>
ol>li*5
</body>
</html>
Now, if I press the Tab key then I get the invalid result:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>A smile 😜</p>
o<ol>
<li></li>
</ol>
</body>
</html>
Pay attention to o before <ol> tag. Also, li item is one, but I expected to get 5 li items... I have the problem for some smiles. For example, I get the problem for 😜 smile, but I haven't for ☠.
const a = '😜';
console.log(`a.length: ${a.length}`); // 2
const b = '☠';
console.log(`b.length: ${b.length}`); // 1
How can I fix it?

Change color of a word in html

I have a .html document that looks like this:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Change color</title>
<style>
.colortext {
color: red;
}
</style>
</head>
<body>
<p>bla bla lbla <span class="colortext">the</span>,
the balfds of .</p>
</body>
</html>
In this document I give to the a red color. But how should I go in order to change color of every thein my huge text? Is there a way to change color in order not to write
<span class="colortext">
for every word, I need?
You can use Javascript to do this. I am not writing the whole code just giving you hints. Get by this var code = document.getElementById("demo") or ByTagName or ByName or any method. Details: https://www.w3schools.com/jsref/met_document_getelementbyid.asp . then replace value "the" with '<span class="colortext">the</span>' using javascript replace method. Details: https://www.w3schools.com/jsref/jsref_replace.asp
Yes with only HTML and CSS that is the only way.
I would use
<font color="red">the</font>
If you want use javascript to achieve this you can use regex like so.
HTML
<p id="sentence">bla bla lbla the,
the balfds of .</p>
Javascript
var sentence = document.getElementById('sentence');
var text = sentence.textContent;
var replaceWord = text.replace(/\bthe\b/g,"<span class='colortext'>$&
</span>")
sentence.innerHTML = replaceWord;

Does minification improve performance?

I have minified my Javascript and CSS files using uglifyJS and the file size on disk has decreased considerably. But on checking the total time for resources loaded in network tab, using Chrome Developer tools, I find there is no difference. So does minification really improve performance? If yes how do I measure it?
Minification can improve performance, depending on your JavaScript engine.
For example, Chrome's V8 optimizing compiler automatically inlines functions less than 600 characters long - including whitespace and comments.
Let's say we have a function which is more than 600 characters long:
function f() {
// A long comment... bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
return 1;
}
Minification reduces this to function f(){return 1}.
If we now call both variants n times and compare the performance of the raw and the minified function, we get the following result:
Obviously, the minified function performs more than twice as fast.
It improves only the size of the JS, so its loading, but nothing else. MinifyJS is probably now not so useful; except if you have a lot of JS scripts to load for your pages. For example, if you use some JS framework or library then it is better to use their minified version, but for your own single script it may not be so interesting.
Minification does not improve execution time.
It however reduces the load time and the number of HTTP requests required by a substantial margin.
http://www.nczonline.net/blog/2009/07/07/javascript-minification-compression-and-performance/
With the modern bandwidths, minification doesn't make any significant difference in total download time, as your own data show. And even in rendering time. What can make a significant difference in total download time are:
The number of files that have to be loaded, especially if they're loaded from other domains.
The size of images, both in terms of dimensions and of compression.
Having your server Gzip large files; see https://css-tricks.com/the-difference-between-minification-and-gzipping/.

HTML to DITA through perl

I am attempting to convert DOCX to DITA topics through an intermediate HTML step.
Now, with simple substitutions either in 'sed' or 'emacs' or 'vi', I can do most of the changes, but not certain types. For that I may need Perl or Python. Below is an example of what I am trying to accomplish:
From:
<h1> Head 1 </H1>
<body>
</body>
<h2>Sub Head 1 </h2>
<body>
</body>
<h3>SubSub Head 1 </h3>
<body>
</body>
<h2>Sub Head 2 </h2>
<body>
</body>
<h1>Head 2 </h1>
<body>
</body>
To:
<topic><title> Head 1 </title>
<body>
</body>
<topic><title> Sub Head 1 </title>
<body>
</body>
<topic><title> SubSub Head 1 </title>
<body>
</body>
</topic>
</topic>
<topic><title> Sub Head 2 </title>
<body>
</body>
</topic>
</topic>
<topic><title> Head 2 </title>
<body>
</body>
</topic>
The part I have trouble with is the part where I need to place the tags for nested topics (and yes, I do have nested topics; my needs are somewhat unique since I am migrating existing documents). If someone can suggest a perl snippet (or a pointer to one similar) for this (placement of tags on a per tag basis), I can build my script around it.
Thanks in advance for looking and suggestions.
That's the kind of processing I often use XML::Twig for.
The wrap_children method is designed just for this: it lets you define a regexp-like expression that will be wrapped in an element. See the example below and the docs for more:
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 1;
use XML::Twig;
# reads the DATA section, the input doc first, then the expected result
my( $in, $expected)= do{ local $/="\n\n"; <DATA>};
my $t=XML::Twig->new->parse( $in);
my $root= $t->root;
# that's where the wrapping occurs, form inside out
$root->wrap_children( '<h3><body>', topic => { level => 3 });
$root->wrap_children( '<h2><body><topic level="3">*', topic => { level => 2 });
$root->wrap_children( '<h1><body><topic level="2">*', topic => { level => 1 });
# now we cleanup: the levels are not used any more
foreach my $to ($t->descendants( 'topic'))
{ $to->del_att( 'level'); }
# the wrapping will have generated tons of additional id's,
# you may not need this if your elements had id's before the wrapping
foreach my $to ($t->descendants( 'topic|body|h1|h2|h3'))
{ $to->del_att( 'id'); }
# now we can deal with titles
foreach my $h ($t->descendants( 'h1|h2|h3')) { $h->set_tag( 'title'); }
# how did we do?
is( $t->sprint( pretty_print => 'indented'), $expected, 'just one test');
__DATA__
<doc>
<h1> Head 1 </h1>
<body></body>
<h2> Sub Head 1 </h2>
<body></body>
<h3> SubSub Head 1 </h3>
<body></body>
<h2> Sub Head 2 </h2>
<body></body>
<h1> Head 2 </h1>
<body></body>
</doc>
<doc>
<topic>
<title> Head 1 </title>
<body></body>
<topic>
<title> Sub Head 1 </title>
<body></body>
<topic>
<title> SubSub Head 1 </title>
<body></body>
</topic>
</topic>
<topic>
<title> Sub Head 2 </title>
<body></body>
</topic>
</topic>
<topic>
<title> Head 2 </title>
<body></body>
</topic>
</doc>