3
\begin{center}
    \begin{tabular}{ |c|c|c| } 
        \hline
        \multicolumn{3}{|c|}{\textbf{Cycle Number and Phase}} \\
        \hline
        t & $ \phi $ & I  \\
        \hline
        6911.68158278 & 0.6843291408376899 & -1
        6911.69533917 & 0.6966511591836024 & -1
        6911.70882358 & 0.7087295567434921 & -1
        6911.7216598  & 0.7202273507382415 & -1
        6911.73451918 & 0.7317458898514815 & -1
        6911.74735541 & 0.7432436928038442 & -1
        6911.76036523 & 0.7548969856170515 & -1
        6911.77317833 & 0.7663740703219446 & -1
        9046.86818608 & 0.23613219262733764 & 1912
        9129.52570639 & 0.27499570945110463 & 1986
        \hline
    \end{tabular}
\end{center}    

For a code like the one shown, I'm trying to make line breaks in each line in order to make it output as a table. There is currently no character after the "-1" or the "1912". But I wish there to be a '\\' after all of that. How do I go about doing so? I tried using the find and replace function that many tex editors have but without a character to replace, I can't seem to do so.

Also, just to clarify, this is actually a 100+ line table with ending values not always equal to -1, so I can't just replace all the -1 with '-1 \\'.

Mico
  • 506,678
Rex Lam
  • 45
  • 3
    With regular expressions, if your editor manages them. Namely, replacing the (invisible) end-of-line character with end-of-line character \\. – Bernard Sep 24 '19 at 19:49
  • Not an actual LaTeX-based solution, but anyway: many Editors (such as VS Code with the LaTeX-Workshop extension) give you the ability to place multiple cursors. By holding shift it creates mutliple cursors over a range of lines (at least VSC). While pressing Ctrl you can easily move with your arrow keys between beginnings/ends of numbers or words simultaneously. This helped me alot in situations like yours. – Wulle Sep 24 '19 at 20:20
  • 1
    If you say which editor you use, we could help more (even if it's sort-of off topic). In vim visual block mode it's quite trivial... – Rmano Sep 24 '19 at 20:34
  • You don't need regular expressions for this, just search for \n (the newline character) and replace with \\\n. In Windows you may need to replace \r\n (carriage [r]eturn line feed, CRLF). In some editors you need to switch on a 'control characters' option with search and replace to find such characters (also for example \t for a tab character), in other editors this is enabled by default. – Marijn Sep 25 '19 at 11:18
  • I use texstudio for most of my latex work. It's pretty nice. But it doesn't have a \n character in the line. That was one of the first things I tried. So in my case, or at the very least, texstudio's case, there literally was no "end-of-line character". Or, the more likely explanation is that I don't know how to find it. – Rex Lam Sep 25 '19 at 17:54

4 Answers4

13

Here's a LuaLaTeX-based solution, which performs the adding of the linebreaks "on the fly", during compilation. It sets up a Lua function called addlinebreak, which simply adds \\ to the end of each line of input, as well as two LaTeX macros, called \addlinebreakOn and \addlinebreakOff. The former macro adds the Lua function to LuaTeX's process_input_buffer callback, and the latter macro removes the Lua function from that callback.

Note where I placed the macros \addlinebreakOn and \addlinebreakOff inside the tabular (actually, array) environment: The former should be placed at the end of the last line that precedes the group of lines which lack \\ directives, and the latter should be placed at the end of the final line that lacks a \\ directive.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}

\usepackage{luacode} % for 'luacode*' environment
\begin{luacode*}
function addlinebreak ( s )
  return s.."\\\\"
end
\end{luacode*}

%% two LaTeX macros:
\newcommand\addlinebreakOn{\directlua{%
   luatexbase.add_to_callback("process_input_buffer", addlinebreak, "addlinebreak" )}}
\newcommand\addlinebreakOff{\directlua{%
   luatexbase.remove_from_callback("process_input_buffer", "addlinebreak" )}}

\begin{document}
\[
  \begin{array}{ |c|c|c| } 
    \hline
    \multicolumn{3}{|c|}{$Cycle Number and Phase$} \\
    \hline
    t & $ \phi $ & I \\
    \hline \addlinebreakOn
    6911.68158278 & 0.6843291408376899 & -1
    6911.69533917 & 0.6966511591836024 & -1
    6911.70882358 & 0.7087295567434921 & -1
    6911.7216598  & 0.7202273507382415 & -1
    6911.73451918 & 0.7317458898514815 & -1
    6911.74735541 & 0.7432436928038442 & -1
    6911.76036523 & 0.7548969856170515 & -1
    6911.77317833 & 0.7663740703219446 & -1
    9046.86818608 & 0.23613219262733764 & 1912
    9129.52570639 & 0.27499570945110463 & 1986 \addlinebreakOff
    \hline
  \end{array}
\]
\end{document}
Mico
  • 506,678
  • 1
    i feel like introducing an entire code and creating a few extra commands seems like a bit of an overkill for just adding extra things at the end, but hey it works. Can't argue with results. Thanks for the help! – Rex Lam Sep 27 '19 at 20:49
8

Just for fun:

\documentclass{article}
\usepackage{pgfplotstable}

\begin{document}

\begin{center}
    \pgfplotstabletypeset[col sep=&, row sep=newline,
      columns/T/.style={column type=|c,column name=t, string type},
      columns/PHI/.style={column type=|c,column name=$\phi$, string type},
      columns/I/.style={column type=|c|,column name=I, int detect, set thousands separator={}},
      every head row/.style={
        before row={
          \hline
          \multicolumn{3}{|c|}{\textbf{Cycle Number and Phase}}\\
          \hline},
        after row={\hline}},
      every last row/.style={
        after row=\hline}]{
    T & PHI & I
    6911.68158278 & 0.6843291408376899 & -1
    6911.69533917 & 0.6966511591836024 & -1
    6911.70882358 & 0.7087295567434921 & -1
    6911.7216598  & 0.7202273507382415 & -1
    6911.73451918 & 0.7317458898514815 & -1
    6911.74735541 & 0.7432436928038442 & -1
    6911.76036523 & 0.7548969856170515 & -1
    6911.77317833 & 0.7663740703219446 & -1
    9046.86818608 & 0.23613219262733764 & 1912
    9129.52570639 & 0.27499570945110463 & 1986
}
\end{center}    
\end{document}

demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • Minor quibble ... in the last column, you've got a hyphen where there should be a minus sign. – barbara beeton Sep 25 '19 at 01:46
  • @barbarabeeton - Alas, a side effect of string type. Otherwise it converts everything to numbers, and I would have to play with the numerical display parameters. – John Kormylo Sep 25 '19 at 14:20
7

The plain TeX approach: Use \obeylines

\begin{center}
  \bgroup
    \let\par=\cr
    \obeylines%
    \begin{tabular}{ |c|c|c| }%
        \hline%
        \multicolumn{3}{|c|}{\textbf{Cycle Number and Phase}}
        \hline%
        t & $ \phi $ & I
        \hline%
        6911.68158278 & 0.6843291408376899 & -1
        6911.69533917 & 0.6966511591836024 & -1
        6911.70882358 & 0.7087295567434921 & -1
        6911.7216598  & 0.7202273507382415 & -1
        6911.73451918 & 0.7317458898514815 & -1
        6911.74735541 & 0.7432436928038442 & -1
        6911.76036523 & 0.7548969856170515 & -1
        6911.77317833 & 0.7663740703219446 & -1
        9046.86818608 & 0.23613219262733764 & 1912
        9129.52570639 & 0.27499570945110463 & 1986
        \hline%
    \end{tabular}%
  \egroup
\end{center}    

I guess this will be frowned about by some LaTeX purists as we go under the hood of the macro package, but it's a very tiny intervention and does not require any external tools or additional packages.

A few words on what the above actually does:

  • After \obeylines, and until the end of the current brace level, every line ending that is not masked by % is translated to \par. Which is normally the same as a blank line, and which we redirect to \cr, which in turn is the same as \\ in a tabular environment.
  • Append % to every line where we do not want to have the said effect.
  • Unfortunately \obeylines cannot appear in the body of {tabular}, so we have to place it outside and add a few more %.

More details about \obeylines etc. can be found in tex/plain/base/plain.tex:517ff

\let\bgroup={ \let\egroup=}

% In \obeylines, we say `\let^^M=\par' instead of `\def^^M{\par}'
% since this allows, for example, `\let\par=\cr \obeylines \halign{...'
{\catcode`\^^M=\active % these lines must end with %
  \gdef\obeylines{\catcode`\^^M\active \let^^M\par}%
  \global\let^^M\par} % this is in case ^^M appears in a \write

(The LaTeX base latex.ltx contains the same definitions, but omits the comment.)

1

This is an emacs answer if you're using that editor. Just run M-x query-replace-regexp and then replace $ by \\\\, possibly selecting first and replacing all.

The idea is that, with regular expressions, the $ character designates the end of line.

Vincent Fourmond
  • 486
  • 2
  • 11
  • You may want to spell out more clearly what you mean by “possibly selecting first and replacing all”. – Mico Sep 25 '19 at 20:57