seth10
seth10

Reputation: 201

\u in JavaScript String.raw`template literal`

How can I avoid \u being interpreted as a character escape sequence?

In my JavaScript project I need a string literal of some beginning LaTeX commands. Before I had every backslash escaped (\\), but I recently discovered raw strings in JavaScript. It would be great if I could have the exact string literal in my source. Unfortunately, there is now an issue in \usepackage{indentfirst}. The \u is interpreted as a character escape sequence, which of course is not what I intend. Is there a way to avoid this behavior, or is this simply not the place for String.raw?

Abridged code:

var title="title", author="author";

console.log(String.raw`
\documentclass{book}

\title{${title}}
\author{${author}}
\date{}

\usepackage{indentfirst}

\begin{document}
\maketitle{}
\tableofcontents{}
`);

I got this to work by replacing the offending line with `+"\\usepackage{indentfirst}"+String.raw`, but of course this is not very clear.

Upvotes: 3

Views: 3980

Answers (1)

seth10
seth10

Reputation: 201

You could take advantage of the \u syntax by replacing the line with \u005Cusepackage{indentfirst}, or even just \x5Cusepackage{indentfirst}.

5C corresponds with \.

Another option might be using the dollar sign and curly braces notation for expressions. ${'\\'}usepackage{indentfirst}

Upvotes: 2

Related Questions