| July 6, 2022
In today’s procrastination effort, I want to generate the LaTeX
code to produce an infinite radical expansion for the number 3, and generate the code using R
.
… inspired by math teacher Chris Smith
First, let me see if I can simply generate the pattern of integers. I will use the letter capital “S” and later replace it with the square root symbol.
stem <- "S 1 + "
print(paste(stem, "8"))
## [1] "S 1 + 8"
for(a in 2:5){
b <- (a+1)^2-1
print(paste(stem, a, "*", b/a))
print(paste(stem, a, "S", (b/a)^2))
print(paste(stem, a, "S", 1, "+", (b/a)^2- 1))
stem <- paste(stem, a, "S", 1, "+")
}
## [1] "S 1 + 2 * 4"
## [1] "S 1 + 2 S 16"
## [1] "S 1 + 2 S 1 + 15"
## [1] "S 1 + 2 S 1 + 3 * 5"
## [1] "S 1 + 2 S 1 + 3 S 25"
## [1] "S 1 + 2 S 1 + 3 S 1 + 24"
## [1] "S 1 + 2 S 1 + 3 S 1 + 4 * 6"
## [1] "S 1 + 2 S 1 + 3 S 1 + 4 S 36"
## [1] "S 1 + 2 S 1 + 3 S 1 + 4 S 1 + 35"
## [1] "S 1 + 2 S 1 + 3 S 1 + 4 S 1 + 5 * 7"
## [1] "S 1 + 2 S 1 + 3 S 1 + 4 S 1 + 5 S 49"
## [1] "S 1 + 2 S 1 + 3 S 1 + 4 S 1 + 5 S 1 + 48"
Not only do I need to now insert the square root symbol, but I would also need to close the parentheses. I think I an literally count the number of times “S” appears and work with that.
stem <- "S1 + "
print("\\sqrt(9)")
## [1] "\\sqrt(9)"
print("\\sqrt(1 + 8)")
## [1] "\\sqrt(1 + 8)"
for(a in 2:5){
b <- (a+1)^2-1
line1 <- paste(stem, a, "*", b/a)
n1 <- stringr::str_count(line1, "S")
line1 <- stringr::str_replace_all(line1, "S", "\\\\sqrt(")
line1 <- paste(line1, paste0(rep(")", n1), collapse = ""), "\\\\")
print(line1)
line2 <- paste(stem, a, "S", (b/a)^2)
n2 <- stringr::str_count(line2, "S")
line2 <- stringr::str_replace_all(line2, "S", "\\\\sqrt(")
line2 <- paste(line2, paste0(rep(")", n2), collapse = ""), "\\\\")
print(line2)
line3 <- paste(stem, a, "S", 1, "+", (b/a)^2- 1)
n3 <- stringr::str_count(line3, "S")
line3 <- stringr::str_replace_all(line3, "S", "\\\\sqrt(")
line3 <- paste(line3, paste0(rep(")", n3), collapse = ""), "\\\\")
print(line3)
stem <- paste0(stem, a, "S", 1, "+")
}
## [1] "\\sqrt(1 + 2 * 4 ) \\\\"
## [1] "\\sqrt(1 + 2 \\sqrt( 16 )) \\\\"
## [1] "\\sqrt(1 + 2 \\sqrt( 1 + 15 )) \\\\"
## [1] "\\sqrt(1 + 2\\sqrt(1+ 3 * 5 )) \\\\"
## [1] "\\sqrt(1 + 2\\sqrt(1+ 3 \\sqrt( 25 ))) \\\\"
## [1] "\\sqrt(1 + 2\\sqrt(1+ 3 \\sqrt( 1 + 24 ))) \\\\"
## [1] "\\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+ 4 * 6 ))) \\\\"
## [1] "\\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+ 4 \\sqrt( 36 )))) \\\\"
## [1] "\\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+ 4 \\sqrt( 1 + 35 )))) \\\\"
## [1] "\\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+4\\sqrt(1+ 5 * 7 )))) \\\\"
## [1] "\\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+4\\sqrt(1+ 5 \\sqrt( 49 ))))) \\\\"
## [1] "\\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+4\\sqrt(1+ 5 \\sqrt( 1 + 48 ))))) \\\\"
Now, I want to move toward a LaTeX array
environment.
print("\\begin{array}{rcl}")
## [1] "\\begin{array}{rcl}"
stem <- "S1 + "
print("3 & = & \\sqrt(9)")
## [1] "3 & = & \\sqrt(9)"
print("3 & = & \\sqrt(1 + 8)")
## [1] "3 & = & \\sqrt(1 + 8)"
for(a in 2:5){
b <- (a+1)^2-1
line1 <- paste(stem, a, "*", b/a)
n1 <- stringr::str_count(line1, "S")
line1 <- stringr::str_replace_all(line1, "S", "\\\\sqrt(")
line1 <- paste(line1, paste0(rep(")", n1), collapse = ""), "\\\\")
print(paste("3 & = &", line1))
line2 <- paste(stem, a, "S", (b/a)^2)
n2 <- stringr::str_count(line2, "S")
line2 <- stringr::str_replace_all(line2, "S", "\\\\sqrt(")
line2 <- paste(line2, paste0(rep(")", n2), collapse = ""), "\\\\")
print(paste("3 & = &", line2))
line3 <- paste(stem, a, "S", 1, "+", (b/a)^2- 1)
n3 <- stringr::str_count(line3, "S")
line3 <- stringr::str_replace_all(line3, "S", "\\\\sqrt(")
line3 <- paste(line3, paste0(rep(")", n3), collapse = ""), "\\\\")
print(paste("3 & = &", line3))
stem <- paste0(stem, a, "S", 1, "+")
}
## [1] "3 & = & \\sqrt(1 + 2 * 4 ) \\\\"
## [1] "3 & = & \\sqrt(1 + 2 \\sqrt( 16 )) \\\\"
## [1] "3 & = & \\sqrt(1 + 2 \\sqrt( 1 + 15 )) \\\\"
## [1] "3 & = & \\sqrt(1 + 2\\sqrt(1+ 3 * 5 )) \\\\"
## [1] "3 & = & \\sqrt(1 + 2\\sqrt(1+ 3 \\sqrt( 25 ))) \\\\"
## [1] "3 & = & \\sqrt(1 + 2\\sqrt(1+ 3 \\sqrt( 1 + 24 ))) \\\\"
## [1] "3 & = & \\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+ 4 * 6 ))) \\\\"
## [1] "3 & = & \\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+ 4 \\sqrt( 36 )))) \\\\"
## [1] "3 & = & \\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+ 4 \\sqrt( 1 + 35 )))) \\\\"
## [1] "3 & = & \\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+4\\sqrt(1+ 5 * 7 )))) \\\\"
## [1] "3 & = & \\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+4\\sqrt(1+ 5 \\sqrt( 49 ))))) \\\\"
## [1] "3 & = & \\sqrt(1 + 2\\sqrt(1+3\\sqrt(1+4\\sqrt(1+ 5 \\sqrt( 1 + 48 ))))) \\\\"
print("\\end{array}")
## [1] "\\end{array}"
Now let’s see if we can collect everything into just one string (of LaTeX code)
main_string <- "\\$\\begin{array}{rcl}"
stem <- "S1 + "
main_string <- paste(main_string, "3 & = & \\sqrt{9} \\\\")
main_string <- paste(main_string, "3 & = & \\sqrt{1 + 8} \\\\")
for(a in 2:7){
b <- (a+1)^2-1
line1 <- paste(stem, a, "*", b/a)
n1 <- stringr::str_count(line1, "S")
line1 <- stringr::str_replace_all(line1, "S", "\\\\sqrt{")
line1 <- paste(line1, paste0(rep("}", n1), collapse = ""), "\\\\")
main_string <- paste(main_string, "3 & = &", line1)
line2 <- paste(stem, a, "S", (b/a)^2)
n2 <- stringr::str_count(line2, "S")
line2 <- stringr::str_replace_all(line2, "S", "\\\\sqrt{")
line2 <- paste(line2, paste0(rep("}", n2), collapse = ""), "\\\\")
main_string <- paste(main_string, "3 & = &", line2)
line3 <- paste(stem, a, "S", 1, "+", (b/a)^2- 1)
n3 <- stringr::str_count(line3, "S")
line3 <- stringr::str_replace_all(line3, "S", "\\\\sqrt{")
line3 <- paste(line3, paste0(rep("}", n3), collapse = ""), "\\\\")
main_string <- paste(main_string, "3 & = &", line3)
stem <- paste0(stem, a, "S", 1, "+")
}
main_string <- paste(main_string, "\\end{array}\\$")
Finally, does this print in inline LaTeX handling here in RStudio??
$ \[\begin{array}{rcl} 3 & = & \sqrt{9} \\ 3 & = & \sqrt{1 + 8} \\ 3 & = & \sqrt{1 + 2 * 4 } \\ 3 & = & \sqrt{1 + 2 \sqrt{ 16 }} \\ 3 & = & \sqrt{1 + 2 \sqrt{ 1 + 15 }} \\ 3 & = & \sqrt{1 + 2\sqrt{1+ 3 * 5 }} \\ 3 & = & \sqrt{1 + 2\sqrt{1+ 3 \sqrt{ 25 }}} \\ 3 & = & \sqrt{1 + 2\sqrt{1+ 3 \sqrt{ 1 + 24 }}} \\ 3 & = & \sqrt{1 + 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