Fixed #516
diff --git a/R/row_spec.R b/R/row_spec.R
index c69a4bc..3d08857 100644
--- a/R/row_spec.R
+++ b/R/row_spec.R
@@ -203,13 +203,14 @@
                                      hline_after, extra_latex_after)
     temp_sub <- ifelse(i == 1 & (table_info$tabular == "longtable" |
                                    !is.null(table_info$repeat_header_latex)),
-                       gsub, sub)
+                       stringr::str_replace_all, stringr::str_replace)
     if (length(new_row) == 1) {
-      out <- temp_sub(target_row, new_row, out, perl = T)
+      out <- temp_sub(out, paste0(target_row, "\\\\\\\\"),
+                      paste0(new_row, "\\\\\\\\"))
       table_info$contents[i] <- new_row
     } else {
-      out <- temp_sub(paste0(target_row, "\\\\\\\\"),
-                  paste(new_row, collapse = ""), out, perl = T)
+      out <- temp_sub(out, paste0(target_row, "\\\\\\\\"),
+                  paste(new_row, collapse = ""))
       table_info$contents[i] <- new_row[1]
     }
   }
diff --git a/R/util.R b/R/util.R
index 4e5274f..785c6ad 100644
--- a/R/util.R
+++ b/R/util.R
@@ -127,7 +127,7 @@
     # insert empty_times before last non whitespace characters
     new_row <- str_replace(
       dup_row, "(?<=\\s)([\\S]+[\\s]*)$",
-      paste0("\\\\\\\\vphantom\\\\{", empty_times, "\\\\}\\1"))
+      paste0("\\\\\\\\vphantom\\\\{", empty_times, "\\\\} \\1"))
     kable_input <- sub(
       paste0(dup_row, "(?=\\s*\\\\\\\\\\*?(\\[.*\\])?)"),
       new_row,