411 lines
		
	
	
		
			9.9 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			411 lines
		
	
	
		
			9.9 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
| {
 | ||
|  "cells": [
 | ||
|   {
 | ||
|    "cell_type": "markdown",
 | ||
|    "metadata": {},
 | ||
|    "source": [
 | ||
|     "# Printing\n",
 | ||
|     "\n",
 | ||
|     "This notebook is focused on how to print numbers with formatting (width, number of digits shown, etc)."
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "markdown",
 | ||
|    "metadata": {},
 | ||
|    "source": [
 | ||
|     "## Load Packages and Extra Functions"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 1,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "data": {
 | ||
|       "text/plain": [
 | ||
|        "printTeXTable (generic function with 2 methods)"
 | ||
|       ]
 | ||
|      },
 | ||
|      "execution_count": 1,
 | ||
|      "metadata": {},
 | ||
|      "output_type": "execute_result"
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "using Printf\n",
 | ||
|     "\n",
 | ||
|     "include(\"jlFiles/printmat.jl\")\n",
 | ||
|     "include(\"jlFiles/printTeXTable.jl\")"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "markdown",
 | ||
|    "metadata": {},
 | ||
|    "source": [
 | ||
|     "## Basic Printing\n",
 | ||
|     "\n",
 | ||
|     "is done with `println()` or `display()`."
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 2,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "name": "stdout",
 | ||
|      "output_type": "stream",
 | ||
|      "text": [
 | ||
|       "[11.11 12.12; 21.0 22.0]\n"
 | ||
|      ]
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "x = [11.11 12.12;21 22]        #a matrix to print\n",
 | ||
|     "\n",
 | ||
|     "println(x)"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 3,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "data": {
 | ||
|       "text/plain": [
 | ||
|        "2×2 Matrix{Float64}:\n",
 | ||
|        " 11.11  12.12\n",
 | ||
|        " 21.0   22.0"
 | ||
|       ]
 | ||
|      },
 | ||
|      "metadata": {},
 | ||
|      "output_type": "display_data"
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "display(x)                     #gives basic formatting"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "markdown",
 | ||
|    "metadata": {},
 | ||
|    "source": [
 | ||
|     "## `printmat()`\n",
 | ||
|     "\n",
 | ||
|     "My simple function `printmat()` allows basic formatting and `printTeXTable()` prints a simple LaTeX table."
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 4,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "name": "stdout",
 | ||
|      "output_type": "stream",
 | ||
|      "text": [
 | ||
|       "  11.11  12.12\n",
 | ||
|       "  21.00  22.00\n",
 | ||
|       "\n"
 | ||
|      ]
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "printmat(x,width=7,prec=2)"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 5,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "name": "stdout",
 | ||
|      "output_type": "stream",
 | ||
|      "text": [
 | ||
|       "           A      B\n",
 | ||
|       "row 1  11.11  12.12\n",
 | ||
|       "row 2  21.00  22.00\n",
 | ||
|       "\n"
 | ||
|      ]
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "colNames = [\"A\" \"B\"]\n",
 | ||
|     "rowNames = [\"row 1\",\"row 2\"]\n",
 | ||
|     "printmat(x,colNames=colNames,rowNames=rowNames,width=7,prec=2)\n",
 | ||
|     "#printmat(x;colNames,rowNames,width=7,prec=2)    #this works too, notice the ;"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 6,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "name": "stdout",
 | ||
|      "output_type": "stream",
 | ||
|      "text": [
 | ||
|       "\\begin{table}\n",
 | ||
|       "  \\begin{tabular}{lrr}\n",
 | ||
|       "     & A & B \\\\ \\hline \n",
 | ||
|       "   row 1 &   11.11 &   12.12 \\\\ \n",
 | ||
|       "   row 2 &   21.00 &   22.00 \\\\ \n",
 | ||
|       "  \\hline\n",
 | ||
|       "  \\end{tabular}\n",
 | ||
|       "\\end{table}\n"
 | ||
|      ]
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "str = printTeXTable(x,colNames=colNames,rowNames=rowNames,width=7,prec=2);"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "markdown",
 | ||
|    "metadata": {},
 | ||
|    "source": [
 | ||
|     "## The PrettyTables.jl Package\n",
 | ||
|     "\n",
 | ||
|     "The [PrettyTables.jl](https://github.com/ronisbr/PrettyTables.jl) package provides powerful methods for formatted printing, including printing to LaTeX.\n",
 | ||
|     "\n",
 | ||
|     "Try also `tf=tf_borderless`, `tf=tf_ascii_dots` or `tf=tf_unicode` (the default) as a keyword argument to modify the table border/lines.\n",
 | ||
|     "\n",
 | ||
|     "To get a witdh of 10 and 3 digits, change to `ft_printf(\"%10.3f\")`."
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 7,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "name": "stdout",
 | ||
|      "output_type": "stream",
 | ||
|      "text": [
 | ||
|       " ------- ------- -------\n",
 | ||
|       " \u001b[1m       \u001b[0m \u001b[1m     A \u001b[0m \u001b[1m     B \u001b[0m\n",
 | ||
|       " ------- ------- -------\n",
 | ||
|       " \u001b[1m row 1 \u001b[0m  11.11   12.12\n",
 | ||
|       " \u001b[1m row 2 \u001b[0m  21.00   22.00\n",
 | ||
|       " ------- ------- -------\n"
 | ||
|      ]
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "using PrettyTables\n",
 | ||
|     "\n",
 | ||
|     "pretty_table(x,colNames,row_names = rowNames,tf=tf_compact,formatters=ft_printf(\"%5.2f\"))  "
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 8,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "name": "stdout",
 | ||
|      "output_type": "stream",
 | ||
|      "text": [
 | ||
|       "\\begin{table}\n",
 | ||
|       "  \\begin{tabular}{rr}\n",
 | ||
|       "    \\hline\\hline\n",
 | ||
|       "    \\textbf{A} & \\textbf{B} \\\\\\hline\n",
 | ||
|       "    11.11 & 12.12 \\\\\n",
 | ||
|       "    21.00 & 22.00 \\\\\\hline\\hline\n",
 | ||
|       "  \\end{tabular}\n",
 | ||
|       "\\end{table}\n"
 | ||
|      ]
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "pretty_table(x,colNames,formatters=ft_printf(\"%5.2f\"),backend=:latex)   #LaTeX table"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "markdown",
 | ||
|    "metadata": {},
 | ||
|    "source": [
 | ||
|     "## Printing Output to a Text File\n",
 | ||
|     "\n",
 | ||
|     "is simple. You `open()` the file, write to it and then `close()` it. The next cells demonstrate this."
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 9,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "name": "stdout",
 | ||
|      "output_type": "stream",
 | ||
|      "text": [
 | ||
|       "NewTxtFile.txt has been created in the subfolder Results. It's still empty.\n"
 | ||
|      ]
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "fh = open(\"Results/NewTxtFile.txt\", \"w\")        #open the file, \"w\" for writing\n",
 | ||
|     "\n",
 | ||
|     "println(\"NewTxtFile.txt has been created in the subfolder Results. It's still empty.\")"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 10,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [],
 | ||
|    "source": [
 | ||
|     "println(fh,\"Dogs are \")          #printing to the file, notice the fh   \n",
 | ||
|     "println(fh,\"nicer than cats.\\n\")\n",
 | ||
|     "\n",
 | ||
|     "printmat(fh,x,prec=2)             #to pretty print the matrix\n",
 | ||
|     "printmat(fh,x,colNames=colNames,rowNames=rowNames,prec=2)\n",
 | ||
|     "printTeXTable(fh,x,colNames=colNames,rowNames=rowNames,prec=2)\n",
 | ||
|     "\n",
 | ||
|     "pretty_table(fh,x,colNames,row_names=rowNames,formatters=ft_printf(\"%5.2f\"))"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 11,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "name": "stdout",
 | ||
|      "output_type": "stream",
 | ||
|      "text": [
 | ||
|       "Results/NewTxtFile.txt has been closed. Check it out. The raw contents are:\n",
 | ||
|       "\n",
 | ||
|       "Dogs are \n",
 | ||
|       "nicer than cats.\n",
 | ||
|       "\n",
 | ||
|       "     11.11     12.12\n",
 | ||
|       "     21.00     22.00\n",
 | ||
|       "\n",
 | ||
|       "              A         B\n",
 | ||
|       "row 1     11.11     12.12\n",
 | ||
|       "row 2     21.00     22.00\n",
 | ||
|       "\n",
 | ||
|       "\\begin{table}\n",
 | ||
|       "  \\begin{tabular}{lrr}\n",
 | ||
|       "     & A & B \\\\ \\hline \n",
 | ||
|       "   row 1 &        11.11 &        12.12 \\\\ \n",
 | ||
|       "   row 2 &        21.00 &        22.00 \\\\ \n",
 | ||
|       "  \\hline\n",
 | ||
|       "  \\end{tabular}\n",
 | ||
|       "\\end{table}\n",
 | ||
|       "┌───────┬───────┬───────┐\n",
 | ||
|       "│       │     A │     B │\n",
 | ||
|       "├───────┼───────┼───────┤\n",
 | ||
|       "│ row 1 │ 11.11 │ 12.12 │\n",
 | ||
|       "│ row 2 │ 21.00 │ 22.00 │\n",
 | ||
|       "└───────┴───────┴───────┘\n",
 | ||
|       "\n"
 | ||
|      ]
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "close(fh)                               #close the file\n",
 | ||
|     "\n",
 | ||
|     "println(\"Results/NewTxtFile.txt has been closed. Check it out. The raw contents are:\\n\")\n",
 | ||
|     "println(read(\"Results/NewTxtFile.txt\",String))"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "markdown",
 | ||
|    "metadata": {},
 | ||
|    "source": [
 | ||
|     "## Formatted Printing (extra)\n",
 | ||
|     "\n",
 | ||
|     "using Printf in Julia 1.6+. \n",
 | ||
|     "\n",
 | ||
|     "(For earlier Julia versions, you may consider the [Formatting.jl](https://github.com/JuliaIO/Formatting.jl) package.)\n",
 | ||
|     "\n",
 | ||
|     "`@printf()` prints to a file or to the screen (and IOBuffers etc), while `@sprintf()` prints to a string. \n",
 | ||
|     "\n",
 | ||
|     "To print with dynamic formatting (that is, where the format is decided at run-time), use `Printf.format()`. "
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 12,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "name": "stdout",
 | ||
|      "output_type": "stream",
 | ||
|      "text": [
 | ||
|       "      3.14\n",
 | ||
|       "my string is just this:  1828 2.71828\n"
 | ||
|      ]
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "z = 3.14159      \n",
 | ||
|     "                                         #the format string is constant\n",
 | ||
|     "@printf(\"%10.2f\\n\",z)                    #width 10, 2 digits, floating point\n",
 | ||
|     "\n",
 | ||
|     "str = @sprintf(\"%5d %g\",1828,exp(1))     #width 5 and integer, compact floating point\n",
 | ||
|     "println(\"my string is just this: \",str)"
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": 13,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [
 | ||
|     {
 | ||
|      "name": "stdout",
 | ||
|      "output_type": "stream",
 | ||
|      "text": [
 | ||
|       "   3.14159\n",
 | ||
|       "    3.1416\n",
 | ||
|       "     3.142\n",
 | ||
|       "     3.142\n",
 | ||
|       "     3.142\n",
 | ||
|       "   3.14159\n"
 | ||
|      ]
 | ||
|     }
 | ||
|    ],
 | ||
|    "source": [
 | ||
|     "for i = 1:6\n",
 | ||
|     "    (width,prec) = (10,rand(0:5))       #the format string is created at run-time, here random\n",
 | ||
|     "    fmt = Printf.Format(\"%$(width).$(prec)f\") \n",
 | ||
|     "    str = Printf.format(fmt,z)\n",
 | ||
|     "    println(str)\n",
 | ||
|     "end    "
 | ||
|    ]
 | ||
|   },
 | ||
|   {
 | ||
|    "cell_type": "code",
 | ||
|    "execution_count": null,
 | ||
|    "metadata": {},
 | ||
|    "outputs": [],
 | ||
|    "source": []
 | ||
|   }
 | ||
|  ],
 | ||
|  "metadata": {
 | ||
|   "@webio": {
 | ||
|    "lastCommId": null,
 | ||
|    "lastKernelId": null
 | ||
|   },
 | ||
|   "anaconda-cloud": {},
 | ||
|   "kernelspec": {
 | ||
|    "display_name": "Julia 1.6.1",
 | ||
|    "language": "julia",
 | ||
|    "name": "julia-1.6"
 | ||
|   },
 | ||
|   "language_info": {
 | ||
|    "file_extension": ".jl",
 | ||
|    "mimetype": "application/julia",
 | ||
|    "name": "julia",
 | ||
|    "version": "1.6.1"
 | ||
|   }
 | ||
|  },
 | ||
|  "nbformat": 4,
 | ||
|  "nbformat_minor": 1
 | ||
| }
 |