Outils pour utilisateurs

Outils du site


issue108:tutoriel2

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, 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 à l'aspect superbe si vous voulez le faire. Plus j'ai travaillé dessus et fait des recherches dans Google sur LaTex et CSV, plus j'ai découvert la souplesse de LaTeX avec un fichier CSV. Mais, pour le moment, je veux créer un répertoire à partir des données d'un 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). Dans l'éditeur, avec l'aide de Rechercher et remplacer, 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 pas que faire cela prendrait longtemps, mais, en réalité, cela nécessitait énormément de dactylographie et beaucoup trop de retours à la ligne. Même si le résultat final était réussi, il y avait forcément une façon plus rapide de le faire. Je travaillais 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 d'une jolie apparence ?

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 « Guelph | 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 \\

Dans la cellule A, le code pour commencer la commande du format en caractère gras « \textbf{ » ; dans la cellule C, la parenthèse fermant la commande du code de format commencé à la cellule A, suivi d'un code de changement de ligne «
». Les cellules E et G n'ont qu'un code de changement de ligne «
», mais elles pourraient avoir plus - je veux rester simple pour le moment.

Sauvez ceci en fichier .CSV et ouvrez-le avec un éditeur de texte ; il devrait ressembler à ceci :

\textbf{,Wellington Law Association,}\\,74 Woolwich St.,\\,Guelph | ON N1H 3T9,

Faites les étapes suivantes de Rechercher et remplacer : • remplacer chaque virgule par une espace, • remplacer « { » par « { » de sorte que « { Wellington » ressemble à « {Wellington », • remplacer le « | » par une virgule. Ça marche dans gEdit, mais pas dans Geany, pour une raison indéterminée.

À la fin, nous avons ceci :

\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.

L'avantage de saisir ainsi le code LaTeX est que vous pouvez copier/coller très rapidement le code nécessaire à votre fichier, pour chaque colonne, plutôt que ligne par ligne comme je l'avais fait la première fois. Vous pouvez commencer par faire un essai avec un fichier d'une seule ligne de données jusqu'à ce que le résultat vous satisfasse, puis copier le code dans le fichier complet.

Une fois que les données ont été manipulées pour un résultat agréable, appuyez sur Ctrl A pour tout sélectionner, Ctrl C pour copier et collez le tout dans un fichier qui contient votre préambule ; enfin, sauvegardez-le dans un fichier .tex. Puis continuez dans votre éditeur LaTeX favori. Pour voir ce que nous pouvons obtenir, voici une des pages du répertoire que j'ai fait :

Vous pouvez prendre les données tabulaires et les formater comme table de plusieurs façons. Je voudrais souligner que ce processus est une façon de prendre les données d'un tableur et les afficher de façon non tabulaire.

Si les données que vous avez sont dans un autre programme, tel que le répertoire de vos contacts de messagerie, vous pouvez probablement les exporter dans un fichier CSV, puis les importer dans un tableur.

Regardons plus précisément un peu du code que j'ai utilisé pour réaliser ce répertoire :

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.

Voici quelques commentaires sur le chouette code du préambule :

\usepackage[paperwidth=3.5in, paperheight=4.5in]{geometry} définit la taille de la page pour les appareils au format de poche.

\setlength{\parindent}{0pt} arrête l'indentation automatique des paragraphes.

\usepackage{hyperref} permet l'utilisation des hyperliens pour les adresses mail et les pages Web.

\usepackage{makeidx}

\makeindex

Ceci permet d'indexer les données sur une page d'index avec la commande \index{ }.

Et dans les données elles-mêmes :

\newpage

j'ai ajouté ceci à la fin de chaque ligne, de façon à avoir une entrée de données par 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

Cette façon de faire transforme LaTeX en un outil rapide et puissant pour afficher les données d'un tableur d'une manière non tabulaire. C'était un moment sensationnel quand je me suis rendu compte que nous n'avons pas toujours besoin de créer un document source LaTeX qui plaise à l'œil humain, même si nous créons un document d'un bel aspect avec du code qui ressemble à de la bouillie pour chiens.

Cependant, après avoir réalisé le document de cette façon, je ne peux que recommander de conserver le fichier de tableur contenant le code LaTeX pour faciliter l'édition des informations dans le futur. Le code que nous avons ainsi créé ne paraît pas joli dans un éditeur LaTeX, mais le résultat est brillant.

Quant à l'écriture de tables en LaTeX à partir d'une feuille de calcul, LaTeX a toutes sortes d'astuces dans son sac que je vous montrerai la prochaine fois.

En attendant, amusez-vous bien en explorant LaTeX.

J'ai aussi créé une vidéo YouTube expliquant la démarche : https://www.youtube.com/watch?v=IBb5wdwACKg

issue108/tutoriel2.txt · Dernière modification : 2016/05/10 14:16 de andre_domenech