RANDOM BITS

A random site by a random clueless human
Random bits of programming, math, and thoughts By a clueless human          Random bits of programming, math, and thoughts By a clueless human

Physics Lab Tex Template

September 28, 2021

I have just finished my first Physics lab in many years and it was quite more time-consuming than I initially thought. This post will go over briefly a few tex commands I found useful when writing my lab along with a link to my Physics template that’ll be subject to change as I write more and more labs.

From Rike ga Koi ni Ochita no de Shoumeishitemita

Title Page

Perhaps your school requires you to have a title page for every lab report you write. Typically it contains the title of the lab, the course code, the date you wrote the lab, the name of your TA and your name.

If you are not provided with a title page written in Tex, you’ll need to script your own title page. That’s where titlepage environment comes in handy.

titlepage is used to indicate everything enclosed is part of your title page and therefore will reserve the page to contain only the contents enclosed in the titlepage environment.

\begin{titlepage}
# Contents of your titlepage
\end{titlepage}

Some very useful commands to use on your title page are the following:

  • \vspace*{1cm} - adds a vertical space (think of it as moving the cursor down) by some specified amount
  • \centerline{} - places the text to the center
  • \hfill <contents> - fills horizontally the remaining space before the content with spaces (i.e. in this specific example, the text is aligned to the right)
  • \begin{flushright} <contents> \end{right} - another alternative to the previous point but this can give you control on where to place the text

Equations

The beauty and power of tex is its ability to typeset mathematical symbols with ease. I won’t be going over the various mathematical symbols and ways to write common math formulas since that’ll make this post too long. What I’ll be focusing on are commands I found very useful or find myself frequently use during my reports. In any paper or textbook you read, you’ll notice that the equations are always numbered so that the author can reference the equation. In a Physics lab, you’ll want to reference your equations quite often to explain concepts or allow readers to quickly go back to the theory section where the equations are derived or explained in more detail during your calculation section. This is done with the use of the equation environment.

Example:

\begin{equation}
  \rho = \frac{m}{V}
\end{equation}

Output: \begin{equation}\label{eq:density} \rho = \frac{m}{V} \end{equation}

Notice how an equation number is assigned even though we never explicitly assigned a number to it. Tex tracks and numbers equations based on when they appear. This feature is very powerful and gives you the flexibility to add equations anywhere at any time without the need to go to every single equation and reassign a number to it. But how do you know what number to reference whenever you are trying to reference a particular equation? That’s where labels come in.

Labels are names you assign to any particular area whether that be a section, equation, figure, or anywhere you want to create a link to. Whenever you want to reference an equation, just use \ref{label} or \hyperref[label]{reference text}. For instance, in the previous example, I actually didn’t show you the entire code snippet. But the actual code I wrote was the following:

\begin{equation}\label{eq:density}
\rho = \frac{m}{V}
\end{equation}

Labels are typically assigned a prefix to indicate what we are referencing such as if it’s a figure, section, table, or an equation. For equations, we use the prefix eq (i.e. eq:label-name). So to reference the density equation, we can use \ref command like so:

Let's substitute the theoretical volume to equation \ref{eq:density}.

Example:

Let's substitute the theoretical volume to equation \ref{eq:density}.

Output: Let’s substitute the theoretical volume to equation \ref{eq:density}.


Another useful command is the text command (\text{...}) when you are in math mode. You’ll probably notice when working with Tex that letters in Math mode are italicized and the fact you cannot write sentences.

$\frac{kg}{cm^3} \text{v.s.} \frac{\text{kg}}{\text{cm3}^3}$ </> $Hello World \text{ v.s.} \text{Hello World}$

Although italicized units are probably not an issue, there are times when you want to add commentary in a step. That’s where the tag command comes in (i.e. \tag{A Commentary}).

Example:

\begin{align*}
    v(t) &= \frac{d}{dt} x(t) \\
    &= \frac{d}{dt} mt + b & \tag{from equation 3} \\
   &= m
\end{align*}

Output: \(\begin{align*} v(t) &= \frac{d}{dt} x(t) \\ &= \frac{d}{dt} mt + b \tag{from equation 3} \\ &= m \end{align*}\)

In the example above, notice how the equal signs line up. This can be achieved through the align* environment and adding & before the equal sign.

Example:

\begin{align*}
    v(t) &= \frac{d}{dt} x(t) \\
    &= \frac{d}{dt} mt + b & \text{from equation 3} \\
   &= m
\end{align*}

Output: \(\begin{align*} v(t) &= \frac{d}{dt} x(t) \\ &= \frac{d}{dt} mt + b & \text{(from equation 3)} \\ &= m \end{align*}\)

What does & mean in Tex? Noice how I achieved a similar result of \tag with & to add a commentary to a step in a math equation. From writing a table (which I will introduce later), & seems to represent a column.

Note: In tex, * tells tex compiler to not number the tag. For instance, writing \subsection without * will create a numbered subsection. If you don’t want a numbered subsection, then a * will be required. Similarly, * is added to align because we don’t want to number each step in the equation since they do not have any significant meaning (i.e. we won’t be referencing that particular step in the future).


Often times when working on a problem or deriving an equation, terms or units tend to cancel each other. It makes it a lot easier for the reader and yourself if you explicitly strike those terms when the situation arises. That’s where the cancel or \bcancel command comes in.

$\require{cancel} m = 5\bcancel{\text{g}} \cdot \frac{1kg}{1000\bcancel{\text{g}}}$

Note: If you are using Mathjax (such as I am for this post, you’ll need to write \require{cancel} enclosed in the same block that is in math mode.

Another useful command when writing your lab or homework is the \boxed{...} command. \boxed is great to enclose your final answer or derivation in a box to catch the readers’ attention.

Example:

\begin{align*}
F &= ma
&= 2 \cdot (-9.8)
&= \boxed{-19.6N}
\end{align*}
\[\begin{align*} F &= ma \\ &= 2 \cdot (-9.8) \\ &= \boxed{-19.6N} \\ \end{align*}\]

Observations and Figures

A lab is not complete with a table of observations nor without figures. In Tex, to create a table, use the tabular environment (i.e. \begin{tabular}{c|c}...\end{tabular}). The parameter to the tabular environment is the number of columns you wish to have and the separator (should there be a line or space to separate the columns). For instance, let’s say we want to create a 3 column table with no borders: Example:

\begin{tabluar}{c c c}
cell1 cell2 cell3 \\
cell3 cell5 cell6 
\end{tabular}

Output: An image of a table written in tex without borders

But if we want to add borders between the columns, append a pipe symbol (| in between the columns). However, we’ll also want a header to the table (i.e. column names). This can be achieved by using \hline to draw a line to separate the headers from the body of the table. Example:

\begin{tabular}{ c|c|c }
id & last & first \\
\hline
1 & Smith & John \\ 
2 & Jane & Mary \\  
\end{tabular}

Output: An example of how a table in tex looks like

Often times we will want to reference tables and have captions onto tables. That’s where \caption and \label come in. A caption allows you to describe the table while label as previously mentioned, assigns a human-readable name to reference since numbering tables is a bad idea. Table labels typically have the prefix tab:<name> (i.e. tab:density). To use a caption and label, it must first be wrapped in another environment called table.

Example:

\begin{table}[H]
\caption{The position of a remotely controlled car strolling down the hall starting from the principle's office} 
\label{tab:position}
\centering
\begin{tabular}{c|c}
t(s) & distance(cm) \\
\hline
1 &  5\\
2 &  7\\
3 & 10 \\
4 & 14 \\
\end{tabular}
\end{table}

A table written in tex with caption and labels

Note: The position parameter ([H]) indicates where to place the contents of the environment. I typically use H to indicate to place the contents of my tables and figures precisely at the location where the TEX code mentions it (you’ll need to add the float package).

Labels and References

I have mentioned labels and references a few times in this post. Labels are not only great for their flexibility to number equations, tables, and figures but also for their ability to create internal document links. In the digital age, all documents should have links to different sections or areas in the document. It gives readers the ability to quickly jump through the document quickly.

One quick note before I give you an example of links inside documents, I want to introduce the command hyperref. For any links you want to create, you’ll need to add the hyperref package (i.e. \usepackage{hyperref}). \hyperref[label]{link text} is a useful command to create a link text that’s more than simply the reference number of the specific material. For instance,

Example:

\hypersetup{
    colorlinks=true,
    urlcolor=purple,
    linkcolor=purple,
}
\begin{document}
\textbf{Equation \ref{eq:density} v.s \hyperref[eq:density]{Table \ref{eq:density}}}
# the rest of the document is ommitted

the difference between a ref and a hyperref in tex

Click on equation \ref{eq:density} to jump to the density equation.


Drawing Boxes Over Variables of Two Equations to Show Relationship

Required Package: \usepackage{colortbl} - for color

There are times when you want to overlap two equations one below the other and draw a box around the variables to show a relationship. One relationship that comes to mind is the Beer-Lambert Law with the calibration curve between the absorbance and the concentration of the color absorbing species.

An example of the relationship between Beer Lambert Law

Inspired by this illustration, I decided to replicate the illustration for a lab report I was writing on determining the acceleration of an object in free fall.

Code:

\begin{tabular}[t]{|c|}
    \arrayrulecolor{red}
    \firsthline
     $A$ \\ $\frac{g}{2}$\\ \lasthline
\end{tabular}
\hspace{-1em}
\begin{tabular}[t]{c}
     $x^2 +$ \\  $t^2$ +\\
\end{tabular}
\hspace{-1em}
\begin{tabular}[t]{|c|}
    \arrayrulecolor{blue}
    \firsthline
     $B$ \\ $v_o$ \\ \lasthline
\end{tabular}
\hspace{-1em}
\begin{tabular}[t]{c}
     $x$ \\ $t$ \\ 
\end{tabular}

Output: The illustration of acceleration with the line of best fit

The idea is to construct tables (one per term) and draw borders on the columns that you wish to highlight. In each cell, it’ll contain multiple lines, a variable per line, separated with a newline from the command \\ and a horizontal line will be drawn over the cell using \firsthline and \lasthline as seen above. To illustrate, if I want to draw the first box, I would begin the “table” with a border and the two variables separated by a newline.

  1. Create a table with one column with a vertical border on the left and right cell (i.e. |c|).

Code:

\begin{tabular}[t]{|c|}
     $A$ \\ 
     $\frac{g}{2}$\\ 
\end{tabular}

Output: The table with two variables from different equations separated by a newline

  1. Draw the top border with \firsthline:

Code:

\begin{tabular}[t]{|c|}
    \firsthline
     $A$ \\ 
     $\frac{g}{2}$\\ 
\end{tabular}

Output: Adding a top border

  1. Draw the bottom border with \lasthline

Code:

\begin{tabular}[t]{|c|}
    \firsthline
     $A$ \\ 
     $\frac{g}{2}$\\
     \lasthline
\end{tabular}

Output:

Adding a top border

  1. Add color to the box with \arrayrulecolor{red}

Code:

\begin{tabular}[t]{|c|}
    \arrayrulecolor{red}
    \firsthline
     $A$ \\ 
     $\frac{g}{2}$\\
     \lasthline
\end{tabular}

Output: Adding color to the borders

  1. Repeat and add another term. If you don’t want to highlight the variable, then omit drawing column borders and drawing the top and bottom borders

Code:

Output: Adding another term but with no focus (i.e. no box around it)

Notice how there’s a lot of space in between the variables. Remove the spacing by using \hspace

Code:

\begin{tabular}[t]{|c|}
    \arrayrulecolor{red}
    \firsthline
     $A$ \\ 
     $\frac{g}{2}$\\
     \lasthline
\end{tabular}
\hspace{-1em}
\begin{tabular}[t]{c}
     $x^2 +$ \\  $t^2$ +\\
\end{tabular}

Output: Removing 1em of horizontal space between the terms


Chemical Reaction

Package: \usepackage[version=4]{mhchem}

To draw a reaction, using mhchem package is useful. For instance,

Code:

% ... everything before
\usepackage[version=4]{mhchem}
\begin{document}
\ce{2H + O_2 -> 2H_2O}
% ... everything after

Output:

H_2O Chemical reaction from H + O_2

Though the reaction arrow is quite long. There are ways to tweak it but I think I’ll keep using this package till I find a need to use another package or method to draw the reaction. (i.e. I have yet to use this package for my labs).


Citation Using BibTex

Often times when writing reports, you need to reference outside knowledge or give acknowledgement of the idea or owner to someone. That is where BibTex comes in handy. BibTex makes citation very simple being able to convert your references in different styles very quickly (i.e. from APA to IEEE for instance). All you need to do is provide a BibTex file (extension: .bib) that contains metadata to your references such as the one below:

@manual{lab,
  title         = "Oscilloscope Experiment",
  organization  = "Carleton University",
  year          = "2022",
  author        = "Maria Paula Rozo Martinez",
},

@manual{uncertainty,
    title = "Measurements and Experimental Uncertainties",
    organization = "Carleton University",
    year = "2022",
    author = "Etienne Rollin"
}

@misc{humidity,
    title = "Static Electricity",
    organization = "Airtec Solutions",
    howpublished = {\url{http//airtecsolutions.com/blog/airtec-blog/airtec-blog/static-electricity}},
    note = {Accessed: 2022-05-27}
}

@article{esd,
    title = {THE ``REAL'' COST OF ESD DAMAGE},
    author = {"Terry Welsher"},
    journal = "InCompliance",
    publisher = "publisher",
    year = "2010",
    month = "June"
}

Then in your Tex File, you can add the following to indicate you wish to have your references generated using a specific style (IEEE in this case) and the bibTex file to look at (i.e. sample.bib):

{\bibliographystyle{IEEEtran}
\bibliography{sample}}

An image of the references section being generated

Intext Citation

Intext citation is very simple using bibtex, all you need to use is the \cite{} command with the tag associated to the reference you wish to cite.

While copper is a great metal due to its low resistivity, the weight and 
cost of copper makes it non-economical for overhead power lines \cite{powerline}

An image of how the in-text citation looks like using IEEE style


Conclusion

I hope the material presented is useful to you. I’ll edit this post once I have time with the link to a generic template I plan on using for my Physics course. It may be useful for anyone who uses a similar structure to CarletonU Physics (I won’t be surprised if my school already has a tex template).

Tex in Fireforce