1

I'm an undergrad in physics and I started using LaTeX last year for my reports.

In every practice, I have a bunch of data collected in Excel, which I usually have to represent (maybe for calculating something using least squares method, maybe just for visualizing it). I like the Excel graph and table format, but I'd like to integrate it in my LaTeX code (without importing it as a PDF).

What packages/tools have some functionality like this? (It doesn't necesarily have to have integration with excel, I can just export my data as a CSV or whatever).

7
  • I like Python and its matplotlib and used it for all my reports (it has a LaTeX backend, so you can create plots which look identical to the rest of your LaTeX document). I know people who like root, but haven't used it myself for anything serious, yet.
    – Skillmon
    Oct 18, 2022 at 10:41
  • 2
    Physics prof here, and my mandi bundle (ctan.org/pkg/mandi) is designed specifically for physics students. It doesn't directly address graphs or tables because I feel those can already be handled better by existing packages (e.g. pgfplots ctan.org/pkg/pgfplots). Oct 18, 2022 at 16:16
  • 1
    BTW, I'm planning to write a book on LaTeX specifically for introductory physics students. Oct 18, 2022 at 16:20
  • For numerical computations the numerica package (ctan.org/pkg/numerica) is wonderful. Oct 18, 2022 at 16:58
  • 1
    I apologize to the original poster for this question being closed as "opinion based," which itself seems to be a matter of opinion as implemented here. Oct 19, 2022 at 9:50

1 Answer 1

3

I recommend the following packages for physics students.

For writing up problem solutions, I humbly recommend the mandi bundle, which I wrote and maintain. It consists of three packages. The first, mandi, typesets physical quantities with the correct units merely by using the quantity’s name. Scalar and vector quantities are supported, as are SI base and derived units as well as alternate units that may not be used in SI. (Note that mandi is not currently compatible with siunitx because the two packages define multiple commands with the same names.) The second, mandistudent, provides, among other things, problem and solution environments as well as support for program listings, with an emphasis on Web VPython (aka GlowScript) and VPython. The third, mandiexp, provides macros for typesetting expressions found in Chabay and Sherwood’s Matter & Interactions but can certainly be used without using that particular text. I have used these materials for nearly a decade.

For doing numerical computations in situ, say, in your written solutions I recommend the numerica package. It parses and preprocesses equations in LaTeX format before passing them to the floating point processor for evaluation.

Many people use other tools for plots but I prefer to use pfgplots and do them in my document because I like having as few files as possible.

Since you are a physics student, here is a graph of the Lorentz factor from special relativity as a function of speed in units of light's speed. There are many, many parameters and settings provided by pgfplots and I encourage you to experiment with them by using some of the commented out lines in the source. Note that I encourage you to use the LuaLaTeX engine.

% !TEX program = lualatexmk
% !TEX encoding = UTF-8 Unicode

\documentclass[border=0pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[%
      declare function = {%
        gamma(\x) = 1/sqrt(1-\x^2);%
        %myx = 0.866;% pick a particular value
        myx = 0.986013;% pick a particular value
      },%
      title style={font=\sffamily},%
        title=Lorentz Factor vs. Speed,%
      label style={font=\sffamily},%
        %xlabel style={font=\sffamily},%
        %ylabel style={font=\sffamily},%
        xlabel=speed \(\frac{v}{c}\),%
        ylabel={Lorentz factor \( \gamma \)},%
        %ylabel style={align=center},ylabel={Lorentz\\ factor \( \gamma \)},%
      tick label style={%
        font=\scriptsize,%
        /pgf/number format/precision=1,%
        /pgf/number format/fixed,%
        /pgf/number format/fixed zerofill,%
      },%
        %xticklabel style={%
        %  /pgf/number format/precision=3,
        %  /pgf/number format/fixed,
        %  /pgf/number format/fixed zerofill,
        %},%
        %yticklabel style={%
        %  /pgf/number format/precision=3,
        %  /pgf/number format/fixed,
        %  /pgf/number format/fixed zerofill,
        %},%
        %xticklabel style={font=\tiny},%
        %yticklabel style={font=\tiny},%
      xtick={0,0.2,...,1},%
      ytick={1,2,...,10},%
        extra x ticks={myx},%
        extra tick style={%
          tick label style={%
            font=\tiny,%
            /pgf/number format/precision=3,%
            /pgf/number format/fixed,%
            /pgf/number format/fixed zerofill,%
            color=red,%
          },%
          xticklabel shift={8pt},%
          yticklabel shift={12pt},%
        },%
        %extra x tick style={%
          %every tick label/.style={font=\tiny},%
          %/pgf/number format/precision=3,%
          %/pgf/number format/fixed,%
          %/pgf/number format/fixed zerofill,%
          %xticklabel shift={8pt},%
        %},%
        extra y ticks={gamma(myx)},%
        extra y tick style={%
        tick label style={rotate=90},%
        %  every tick label/.style={font=\tiny},%
        %  /pgf/number format/precision=3,%
        %  /pgf/number format/fixed,%
        %  /pgf/number format/fixed zerofill,%
        %  yticklabel shift={25pt},%
        },%
      xmin=0,%
      xmax=1.05,%
      ymin=0,%
      ymax=10,%
      %domain=0:0.999, samples=200,% this affects actual plotting
    ]%
    %-- main plot
    %\addplot[blue, very thin, smooth] {1/sqrt(1-x^2)};
    \addplot[blue, very thin, smooth, domain=0:0.999, samples=200] {gamma(x)};
    \addplot[blue, mark=*, mark size=0.5pt] ({myx},{gamma(myx)});
    %-- vertical asymptote
    \draw[red, very thin, densely dashed] (1,0) -- (1,\pgfkeysvalueof{/pgfplots/ymax});
    %-- horizontal asymptote
    %\draw[red, very thin, densely dashed] (0,1) -- (\pgfkeysvalueof{/pgfplots/xmax},1);
    %-- particular value
    %\draw[red, very thin] (0,2.294) -- (0.9,2.294);
    %\draw[red, very thin] (0.9,0) -- (0.9,2.294);
    \draw[red, very thin] (0,{gamma(myx)}) -- (myx,{gamma(myx)});
    \draw[red, very thin] (myx,0) -- (myx,{gamma(myx)});
  \end{axis}
\end{tikzpicture}
\end{document}

Plot of the relativistic Lorentz factor as a functionof speed

And here is another example showing how to plot a function, in this case a quadratic, and a tangent line at a point you can select by changing one variable as in the Lorentz factor plot above.

% !TEX program = lualatexmk
% !TEX encoding = UTF-8 Unicode

\documentclass[border=0pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[%
      declare function = {%
        f(\x) = \x^2; % function
        df(\x) = 2*\x;% derivative
        myx = 2.5;    % pick a particular value
        dx = 0.001;   % increment
      },%
      title style={font=\sffamily},%
        title=A Quadratic Function,%
      label style={font=\sffamily},%
      axis lines=left,%
        xlabel={\( x \)},%
        ylabel={\( f(x) \)},%
      tick label style={%
        font=\scriptsize,%
        /pgf/number format/precision=1,%
        /pgf/number format/fixed,%
        /pgf/number format/fixed zerofill,%
      },%
      xtick={-5,-4,...,5},%
      ytick={0,2,...,20},%
      xmin=-5, xmax=5.5,%
      ymin=0,  ymax=20.5,%
      extra x ticks={myx},%
      extra y ticks={f(myx)},%
      extra tick style={%
        tick style=red,%
        tick label style={%
          font=\tiny,%
          color=red,%
          /pgf/number format/precision=3,%
          /pgf/number format/fixed,%
          /pgf/number format/fixed zerofill,%
        },%
        xticklabel shift={8pt},%
        yticklabel shift={16pt},%
      },%
      extra y tick style={%
        tick label style={rotate=90},%
      },%
    ]%
    %-- main plot
    \addplot[blue, very thin, smooth, domain=-5:5, samples=100] {f(x)};
    \addplot[blue, mark=*, mark size=0.5pt] ({myx},{f(myx)});
    %-- particular value
    \draw[red, very thin] (-5,{f(myx)}) -- (myx,{f(myx)});
    \draw[red, very thin] (myx,0) -- (myx,{f(myx)});
    %-- tangent line
    \draw[green, thick, shorten >=-1cm, shorten <=-1cm] ({myx-dx},{f(myx-dx)}) -- ({myx+dx},{f(myx+dx)});
  \end{axis}
\end{tikzpicture}
\end{document}

Plot of a quadratic function and its tangent at a point.

Finally, here's an example of including plots in a document. The first is created in situ and the second is created separately and imported as a figure. The plot is the Lorentz factor plot from above. For this example, it was simply named plot.pdf and located in the same folder as the document.

% !TEX program = lualatexmk
% !TEX encoding = UTF-8 Unicode

\documentclass{article}
\usepackage{float}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
Let's create a plot of the Lorentz factor as a function of speed.
Let's begin by creating the plot in situ.
\begin{figure}[H]
\centering
\begin{tikzpicture}
  \begin{axis}[%
      declare function = {%
        gamma(\x) = 1/sqrt(1-\x^2);%
        %myx = 0.866;% pick a particular value
        myx = 0.986013;% pick a particular value
      },%
      title style={font=\sffamily},%
        title=Lorentz Factor vs. Speed,%
      label style={font=\sffamily},%
        %xlabel style={font=\sffamily},%
        %ylabel style={font=\sffamily},%
        xlabel=speed \(\frac{v}{c}\),%
        ylabel={Lorentz factor \( \gamma \)},%
        %ylabel style={align=center},ylabel={Lorentz\\ factor \( \gamma \)},%
      tick label style={%
        font=\scriptsize,%
        /pgf/number format/precision=1,%
        /pgf/number format/fixed,%
        /pgf/number format/fixed zerofill,%
      },%
        %xticklabel style={%
        %  /pgf/number format/precision=3,
        %  /pgf/number format/fixed,
        %  /pgf/number format/fixed zerofill,
        %},%
        %yticklabel style={%
        %  /pgf/number format/precision=3,
        %  /pgf/number format/fixed,
        %  /pgf/number format/fixed zerofill,
        %},%
        %xticklabel style={font=\tiny},%
        %yticklabel style={font=\tiny},%
      xtick={0,0.2,...,1},%
      ytick={1,2,...,10},%
        extra x ticks={myx},%
        extra tick style={%
          tick label style={%
            font=\tiny,%
            /pgf/number format/precision=3,%
            /pgf/number format/fixed,%
            /pgf/number format/fixed zerofill,%
            color=red,%
          },%
          xticklabel shift={8pt},%
          yticklabel shift={12pt},%
        },%
        %extra x tick style={%
          %every tick label/.style={font=\tiny},%
          %/pgf/number format/precision=3,%
          %/pgf/number format/fixed,%
          %/pgf/number format/fixed zerofill,%
          %xticklabel shift={8pt},%
        %},%
        extra y ticks={gamma(myx)},%
        extra y tick style={%
        tick label style={rotate=90},%
        %  every tick label/.style={font=\tiny},%
        %  /pgf/number format/precision=3,%
        %  /pgf/number format/fixed,%
        %  /pgf/number format/fixed zerofill,%
        %  yticklabel shift={25pt},%
        },%
      xmin=0,%
      xmax=1.05,%
      ymin=0,%
      ymax=10,%
      %domain=0:0.999, samples=200,% this affects actual plotting
    ]%
    %-- main plot
    %\addplot[blue, very thin, smooth] {1/sqrt(1-x^2)};
    \addplot[blue, very thin, smooth, domain=0:0.999, samples=200] {gamma(x)};
    \addplot[blue, mark=*, mark size=0.5pt] ({myx},{gamma(myx)});
    %-- vertical asymptote
    \draw[red, very thin, densely dashed] (1,0) -- (1,\pgfkeysvalueof{/pgfplots/ymax});
    %-- horizontal asymptote
    %\draw[red, very thin, densely dashed] (0,1) -- (\pgfkeysvalueof{/pgfplots/xmax},1);
    %-- particular value
    %\draw[red, very thin] (0,2.294) -- (0.9,2.294);
    %\draw[red, very thin] (0.9,0) -- (0.9,2.294);
    \draw[red, very thin] (0,{gamma(myx)}) -- (myx,{gamma(myx)});
    \draw[red, very thin] (myx,0) -- (myx,{gamma(myx)});
  \end{axis}
\end{tikzpicture}
\caption{Plot of Lorentz factor as a function of speed..}
\label{plot:1}
\end{figure}

Figure \ref{plot:1} is a great graph. Notice the asymptotic behavior as speed 
approaches \(1\) (light's speed).

\newpage
Now let's import the graph from a file. Figure \ref{plot:2} was created externally and 
imported as an image.
\begin{figure}[H]
  \centering
  \includegraphics{plot.pdf}
  \caption{Plot of Lorentz factor as a function of speed.}
\label{plot:2}
\end{figure}
As you can see, the two plots are identical. 
\end{document}

The output has two pages.

Page one of the output.

Page two of the output.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .