5 Example Text with Code

Here is an example of a code chunk that creates a Log Scale.

plot(0,0,ty="n",xlim=c(0,10),ylim=c(0,3),xlab="Length along rule [inches]",ylab="",yaxt="n" ,
     main="Making a Log Scale")
text(10*log10(c(1:10)),c(1:10)*0+2,c(1:10))
abline(h=1.4)
segments(10*log10(c(1:10)),1,10*log10(c(1:10)),1.8,lwd=2,col="blue")
segments(10*log10(seq(1,10,0.1 )),1.3, 10*log10(seq(1,10, 0.1)),1.5, lwd=1,col="blue")
segments(10*log10(seq(1,10,0.5 )),1.2, 10*log10(seq(1,10, 0.5)),1.6, lwd=2,col="blue")
segments(10*log10(seq(1, 2,0.2)),1.35,10*log10(seq(1, 2,0.2)),1.45,lwd=1,col="blue")
segments(10*log10(seq(1, 2,0.02)),1.37,10*log10(seq(1, 2,0.02)),1.43,lwd=1,col="blue")
text(2,2.5,"Logarithmic spacing:")
Exmpls = c(1.13, 1.65,3.8,7.1,9.6)
segments(10*log10(Exmpls),0.5,10*log10(Exmpls),1.3,lwd=1,col="red")
text(10*log10(Exmpls),0.3,as.character(Exmpls))

We can insert an image from a file like this:

Mannheim-style K&E Slide Rule, Model 4058W.

We can also compute numbers and insert the results directly into the text. For example, suppose we want to initiate values for variables \(x\) and \(y\), and then compute and display within the text the result:

\[ z = \sqrt{x^2 + y^2}. \] (Note that the equation and its display is written in standard \(\LaTeX\) format.1)

x = 55
y = 148
z = sqrt(x^2 + y^2)

The final result for the variable \(z\) has a value of 157.89, rounded to two decimal places, which you see is directly embedded and appropriately displayed in the final text.