Ceci est une ancienne révision du document !
Last year, I created a member directory for the Ontario Courthouse Library Association, of which I am a member. Since all of the information is in a spreadsheet, I thought that it would not take too long to make a nifty little membership directory in LaTeX that was formatted to look good on a computer screen, tablet, or a smartphone; in short – convergence ready. You may have data that is in a database or spreadsheet that you would like to extract and display in a different format. LaTeX can display fantastic looking tables if you want to do that. The more I worked on this and made Google searches on LaTeX and CSV, I found out just how versatile LaTeX can be with a CSV file. But for now, I want to create a directory from data in a spreadsheet. Since my data was in a LibreOffice Calc file, I clicked on File > Save as > All Formats BOX > Text CSV (.csv). With the aid of search and replace in my text editor, I was able to eliminate all of the commas by replacing them with a space. Then, after creating a preamble in my LaTeX editor, I copied and pasted my data into a LaTeX file and proceeded to insert the LaTeX code, line by line, until my data looked like that shown above.
L'an dernier, j'ai créé un répertoire des membres de l'association de la bibliothèque du palais de justice de l'Ontario, dont je suis membre. Comme toute l'information est dans un tableur, je pensais que ça ne prendrait pas beaucoup de temps de faire un chouette petit répertoire des membres en LaTeX, qui serait formaté pour avoir un air sympa sur un écran d'ordinateur, sur une tablette ou un smartphone ; en clair, prêt pour la convergence.
Vous pouvez avoir des données stockées dans une base de données ou un tableur, que vous voudriez extraire pour les afficher dans un format différent. LaTeX peut afficher des tables au superbe aspect si vous voulez le faire. Plus j'ai travaillé dessus et fait des recherches dans Google sur LaTex et CSV, j'ai découvert simplement la souplesse de LaTeX avec un fichier CSV. Mais, pour le moment, je veux créer un répertoire à partir des données du tableur.
Comme mes données sont dans un fichier LibreOffice Calc, j'ai cliqué sur Fichier > Enregistrer sous > Liste déroulante Tous formats > Texte CSV (.csv). Avec l'aide Rechercher et remplacer dans l'éditeur, j'ai pu éliminer toutes les virgules en les remplaçant par une espace. Ensuite, après avoir créé un préambule dans mon éditeur LaTeX, j'ai copié/collé mes données dans le fichier LaTeX et procédé à l'insertion du code LaTeX, ligne par ligne, jusqu'à ce que mes données ressemblent à ce qui est présenté ci-dessus.
I did not think that it would take a long time to do this, but, as it turned out, this ended up being a lot of typing, heck, it had way too many line returns. Even though the finished product was a success there had to be a faster way to do this. I was working to make the code easier to read for humans, which is good, but what if LaTeX and my computer did not need it to look that way? Let's go back to the spread-sheet, and, for the purpose of this article, we will just look at the first three columns, and insert an empty column to the left of every column so it looks like the image above. Before we go any further, do a search and replace on the entire file replacing any commas with a bar “ | ” and a space before and after it. This is to give a new separation character between city and province so that “Guelph, ON” becomes “Guelph | ON”. Into the cells, type in the code needed for each line of text (shown below).
Je n'imaginais pa que ça prendrait un temps aussi long pour le faire, comme ça a été le cas, avec beaucoup de frappe, énormément, et beaucoup trop de retours à la ligne. Même si le résultat final a été un succès, il devait y avoir un façon plus rapide de le faire. J'ai travaillé pour rendre le code plus facile à lire pour des humains, ce qui est bien, mais que se passerait-il si LaTeX et mon ordinateur n'avaient pas besoin que ça ressemble à ça ?
Revenons au tableur et, pour les besoins de cet article, nous regarderons seulement les trois premières colonnes, avec l'insertion d'une colonne vide à gauche de chacune d'elles, de sorte que ça ressemble à l'image ci-dessus.
Avant d'aller plus avant, faites un Rechercher et remplacer sur tout le fichier pour substituer à chaque virgule une barre « | » précédée et suivie d'une espace. Ceci pour disposer d'un nouveau séparateur entre ville et province, de sorte que « Guelph, ON » devienne « Guelth | ON ».
Dans les cellules, tapez le code nécessaire à chaque ligne de texte (voir ci-dessous).
Cell A has the code to begin the boldface format command “\textbf{“, cell C has the bracket to close the format code command started in cell A “}” with the line break code added as “\\”. Cells E and G have only line break code “\\”, but they could have more – for now I want to keep it simple.
Save this as a .CSV file and open with a text editor and it should look like this:
\textbf{,Wellington Law Association,}\\,74 Woolwich St.,\\,Guelph | ON N1H 3T9,
Do the following search and replace steps:
• replace each comma with a space
• replace the “{ “ with “{“ so that the “{ Wellington looks like “{Wellington
• replace the “ | ” with a comma.
(It works in gEdit, but it did not in Geany for some reason.)
We end up with this:
\textbf{Wellington Law Association }
74 Woolwich St.
Guelph, ON N1H 3T9 \\
The advantage of entering LaTeX code this way is that you can copy and paste or paint the needed code for your file very quickly, for each column, instead of line by line like I did the first time around. You can start off by experimenting with a file made up of one row of data until you are happy with it, and then copy the code into the full data file. After the data is manipulated to look pretty, press Ctrl A to select all, Ctrl C to copy, and then paste it into a file that holds your preamble and then save the file as a .tex file. Then process it through your favourite LaTeX editor. To see what we can end up with, here is a page from the directory I made: There are many different methods to take tabular data and process it in a table format. I want to emphasise that this process is a method to take spreadsheet data and display it in a non-tabular way. If the data that you have is in another program such as an e-mail client's directory, you can probably export the data as a CSV file and then import it into a spreadsheet. Let’s highlight some of the code I used to make this directory.
Here are some comments on the nifty code in the preamble: \usepackage[paperwidth=3.5in, paperheight=4.5in]{geometry} sets a page size that is ideal for hand-held devices \setlength{\parindent}{0pt} stops automatic paragraph indents \usepackage{hyperref} allows the use of hyperlinks for e-mail and web pages \usepackage{makeidx} \makeindex This lets us index data on an index page with the \index{ } command And in the data itself: \newpage I added this to the end of every row, resulting in one record per page.
This process turns LaTeX into a very fast and powerful tool for displaying spreadsheet data in a non-tabular way. It was one of those “A HA!!” moments when I realised that we do not always need to create a LaTeX source document that caters to the human eye, even though we create a beautiful looking document with code that looks like a dog's breakfast. However, after we produce a document this way, it may be advisable to keep the spreadsheet file that contains the LaTeX code as this will make it easier to edit the information at a later date. The code that we create this way does not look pretty in the LaTeX editor, but the end results look great. As for writing tables in LaTeX from spreadsheet data, LaTeX has all kinds of tricks in the lion's den and I will look into that next time. Until next time, enjoy exploring LaTeX. I've also created a YouTube video explaining this procedure: https://www.youtube.com/watch?v=IBb5wdwACKg