42144

Replace accents in string vector with Latex code

Question:

Define:

df <- data.frame(name=c("México","Michoacán"),dat=c(1,2))

s.t.

> df name dat 1 México 1 2 Michoacán 2

When I print this table to a .tex file using xtable the accented characters get garbled, which is no surprise.

I would like to replace accents with proper Latex formatting e.g.:

> df name dat 1 M\'{e}xico 1 2 Michoac\'{a}n 2

Please note in real dataset there are many different names with different accented letters but all with same type of accent (i.e. foward-slash), so the only thing that needs to change in \'{.} is the letter in place of the dot.

In trying one reader's suggestion i did the following:

> df <- data.frame(name=c("México","Michoacán"),dat=c(1,2)) > df name dat 1 México 1 2 Michoacán 2 > df$name <- sub("é", "\\\\'{e}", df$name,) > df name dat 1 M\\'{e}xico 1 2 Michoacán 2 > capture.output( + print(xtable(df)), + file = "../paper/rTables.tex", append = FALSE)

When I opened the <em>rTables.tex</em> file in Notepad:

% latex table generated in R 2.13.1 by xtable 1.5-6 package % Fri Jul 15 13:19:17 2011 \begin{table}[ht] \begin{center} \begin{tabular}{rlr} \hline & name & dat \\ \hline 1 & M$\backslash$'\{e\}xico & 1.00 \\ 2 & Michoacán & 2.00 \\ \hline \end{tabular} \end{center} \end{table}

This is not what is needed.

Answer1:

I think the problem is that this case is asking a lot of xtable's attempts to convert strange characters to LaTeX. Try overriding sanitize.text.function as follows:

print(xtable(df),sanitize.text.function=function(x){x})

which on my system outputs this:

% latex table generated in R 2.13.0 by xtable 1.5-6 package % Fri Jul 15 10:30:00 2011 \begin{table}[ht] \begin{center} \begin{tabular}{rlr} \hline & name & dat \\ \hline 1 & M\'{e}xico & 1.00 \\ 2 & Michoacán & 2.00 \\ \hline \end{tabular} \end{center} \end{table}

It might be that other LaTeX markup may be broken by doing this, though, so be aware of that.

Answer2:

Use the stringr package, and replace each type of accented character one at a time.

library(stringr) df$name <- str_replace_all(df$name, "é", "\\\\'{e}") df$name <- str_replace_all(df$name, "á", "\\\\'{a}") df$name

Recommend

  • Is there some way to go on the previous page get the pressed button text?
  • XSL value-of select isn't working
  • Windows Command line get date
  • Plotting to a file from a function in R
  • Replacing accents w/ their counterparts in AS3
  • Python email payload decoding
  • How to read xlsx file in an android application?
  • Formatting with mako
  • draw a B+ tree in latex
  • Ruby regex to remove all consecutive letters from string
  • Is a .txt file created in VB different than one I'd randomly create?
  • What I have to do to guarantee that ccc.jar is loaded before aaa.jar?
  • Can you use DataSet and DataTables in a Portable Class Library
  • Add spaces between words in spaceless string
  • How does ActiveSupport do month sums?
  • Layout design help Android
  • AlertDialog style when using setView()
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • xtable - background colour of added rows
  • ZipList with Scalaz
  • Android cannot disable cut copy paste
  • How to return DataSet (xsd) in WCF
  • how does System.Web.HttpRequest::PathInfo work?
  • Breaking out column by groups in Pandas
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • Installed module is empty
  • Time complexity of a program which involves multiple variables
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • Akka Routing: Reply's send to router ends up as dead letters
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • json Serialization in asp
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How do I configure my settings file to work with unit tests?
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs
  • Unable to use reactive element in my shiny app