Package: labtex
labtexNew extensiveDocumentation!PurposeThis package provides a single solution to repetitive analysis tasks in a lab environment. If you are doing error propagation, linear regression or LaTeX tables/figures manually, this package automates the process.FeaturesMeasurement and MeasurementList classes with automatic:Error propagationPrinting to correct significant figuresUnit parsing, propagation and conversionLinear regression and nonlinear curve fittingA Document class with automatic:LaTeX table generation in two different stylesPlot generation with matplotlibInsertion/updating of tables and figures into a LaTeX fileInstallationUsingpiporpip3the latest release can be installed withpip3 install labtexUsageFor ease of use, you can import the package into your file's global namespace withfromlabteximport*The rest of this section will assume the package is imported in this way. Alternatively doimport labtex as lt.Single measurements can be instantiated withMeasurement(value,uncertainty,unit)where unit is a string that will be parsed.x=Measurement(1.1,0.3,"m")y=Measurement(2.22,0.4,"m")z=M(314,10,"V")whereMis an equivalent shorthand forMeasurement. Note that the unit parsing supports all combinations of common units, prefixes and powers of units, eg. any of "nm^2", "C^-1", "kg m^2 s^-2", "J^3" etc. are supported.Measurement instances support all operations (+-*/and**) as well as math functions with the error and units automatically propagated. Some examples are shown below.print(x)# 1.1 ± 0.3 mprint(x+y)# 3.3 ± 0.5 mprint(x*z)# (35 ± 9) × 10^{1} V mprint(x**2)# 1.2 ± 0.7 m^2print(Measurement.tan(x/y))# 0.5 ± 0.2Notice also that Measurements are rounded to the significant figures as dictated by the uncertainty.For a list of measurements, theMeasurementListclass functions identically to theMeasurementclass, only now taking a list of values. The uncertainty can be a list or a single value for all measurements.heights=MeasurementList([185,183,182,194,184,177],5,"cm")# Note: One could use the shorthand class ML(values,uncertainty,unit)print(heights)# [185 ± 5, 183 ± 5, 182 ± 5, 194 ± 5, 184 ± 5, 177 ± 5] cmprint(200-heights)# [15 ± 5, 17 ± 5, 18 ± 5, 6 ± 5, 16 ± 5, 23 ± 5] cmMeasurementLists also support all operations (+-*/and**) with themselves and withMeasurements.With twoMeasurementListinstances, they can be linearly regressed with theLinearRegressionclass.voltages=MeasurementList([1.3,3,5,7,8.5,10],1,"V")temperatures=MeasurementList([23,55,67,82,88,96],[5,3,7,10,5,6],"K")reg=LinearRegression(voltages,temperatures)print(lobf)# m = 7 ± 1 V^{-1} C# c = 27 ± 7 CObserve that printing allMeasurementandMeasurementListinstances rounds the value to the largest significant figure of the error, as is convention.To visualise this linear regression, theplotmethod can be used.fig=linreg.plot(xlabel="Voltage, V",ylabel="Temperature, T",title="Voltage and Temperature Plot")fig.show()This generates the following plot, in which the line of best fit is shown, as well as the region of uncertainty (in the linear regression parameters). Notice that the units are automatically included in the axis labels.In order to include such a figure or a table of the data used in a LaTeX document, theDocumentclass can be used. Argument names are not required, they are shown here only for demonstration.doc=Document(title="Lab Report Template",author="CianLM",filename="report.tex")Note thatfilenamehere can be a preexisting.texfile or a new one to be created by labtex.Oncedochas been instantiated, tables and figures may be added to the document with their respective methods. Once again argument names are for demonstration and most are optional anyway.doc.add_table(nameandsymbol=["Voltage, V","Temperature, T"],data=[voltages,temperatures],caption="Voltage and Temperature Correlation")This inserts the following into thedocinstance.\begin{table}[ht]\centering\caption{Voltage and Temperature Correlation}\label{tab:1}\begin{tabular}{c|cccccc}\topruleVoltage, V, ($\pm1$V)&1&3&5&7&8&10\\Temperature, T&$23\pm5$&$55\pm3$&$67\pm7$&$(8\pm1)\times10^{1}$&$88\pm5$&$96\pm6$\\\bottomrule\end{tabular}\end{table}which results inAlternatively if anuprighttable is preferred, this may be specified through thestyleargument.doc.add_table(nameandsymbol=["Voltage, V","Temperature, T"],data=[voltages,temperatures**2],caption="Voltage and Temperature Squared Correlation",style="upright")Once again, this inserts the following into thedocinstance.\begin{table}[ht]\centering\caption{Voltage and Temperature Squared Correlation}\label{tab:2}\begin{tabular}{*{2}c}\topruleVoltage, V, ($\pm1$V)&Temperature, T\\\midrule1&$(5\pm2)\times10^{2}$\\3&$(30\pm3)\times10^{2}$\\5&$(45\pm9)\times10^{2}$\\7&$(7\pm2)\times10^{3}$\\8&$(77\pm9)\times10^{2}$\\10&$(9\pm1)\times10^{3}$\\\bottomrule\end{tabular}\end{table}which results inFor a figure, a similar process occurs. Given any figure object, such as the one generated bylinreg.plot()above, it can be added with thedoc.add_figuremethod. For example, one could dosquarereg=LinearRegression(voltages,temperatures**2)fig=squarereg.plot(xlabel="Voltage, V",ylabel="Temperature Squared, $T^2$",title="Voltage and Temperature Squared Plot")doc.add_figure(fig,caption="Linear Regression of Voltage and Temperature Squared")# labtex: Wrote to 'figures/graph1.png'.This generates the figure below and saves it tofigures/graph1.pngwhere the1will increment with each figure you add.If you want a different filename, you can specify with thefilename=...argument and if you want your graphs elsewhere thanfigures/, you may changeDocument.graphfolderat your convenience.Once you have added all your tables and graphs to thedocobject, you may save this file as shown below. The default write directory istex/relative to root. This directory is also customizable withDocument.texfolder.doc.save()# labtex: Wrote to 'tex/report.tex'.You can then edit the generated.texfile freely, and run the python file again at any point to add/edit/remove tables and figures. labtex will automatically update the.texfile with your changes.By default, tables will be inserted after a line starting with%labtex-tablesand figures will be inserted after a line starting with%labtex-figures. If you want to change the comments labtex looks for, you can edit theDocument.tablemarkerandDocument.figuremarkerattributes respectively. If such comments are not found, tables/figures will be inserted at the very end of the document.For more advanced usage and guides (such as the creation of the plot below) see theDocumentation.ContributionsThis package is under active development. Feel free to submit a pull request or reach out with feature suggestions.