| 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 |
%% wordcloud %% |
| 3 |
%% drawing wordclouds %% |
| 4 |
%% with METAPOST and Lua %% |
| 5 |
%% [email protected] %% |
| 6 |
%% Version 0.3 (novembre 2025) %% |
| 7 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 8 |
% |
| 9 |
% This work may be distributed and/or modified under the conditions of |
| 10 |
% the LaTeX Project Public License, either version 1.3c of this license |
| 11 |
% or (at your option) any later version. The latest version of this |
| 12 |
% license is in http://www.latex-project.org/lppl.txt |
| 13 |
% |
| 14 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 15 |
\NeedsTeXFormat{LaTeX2e}% |
| 16 |
\def\PackageName{wordcloud}% |
| 17 |
\def\fileversion{v0.2}% |
| 18 |
\def\filedate{2023/07/26}% |
| 19 |
\ProvidesPackage{wordcloud}[\filedate\space\fileversion]% |
| 20 |
% |
| 21 |
% package to load |
| 22 |
\RequirePackage{luamplib}% |
| 23 |
\mplibtextextlabel{enable} % to allow LaTeX in list of words inside LaTeX document |
| 24 |
% |
| 25 |
% load the lua code |
| 26 |
\directlua{require("wordcloud.lua")}% |
| 27 |
|
| 28 |
\ExplSyntaxOn |
| 29 |
|
| 30 |
% variable globale |
| 31 |
\tl_new:N \g__wc_wordcloud_scale_tl |
| 32 |
\tl_new:N \g__wc_wordcloud_rotate_tl |
| 33 |
\tl_new:N \g__wc_wordcloud_margin_tl |
| 34 |
|
| 35 |
% definitions des clés/valeurs pour la commande \wordcloud |
| 36 |
\keys_define:nn{wc/wordcloud}{% |
| 37 |
scale .tl_gset:N = \g__wc_wordcloud_scale_tl, |
| 38 |
scale .value_required:n = true, |
| 39 |
rotate .tl_gset:N = \g__wc_wordcloud_rotate_tl, |
| 40 |
rotate .value_required:n = true, |
| 41 |
margin .tl_gset:N = \g__wc_wordcloud_margin_tl, |
| 42 |
margin .value_required:n = true, |
| 43 |
usecolor .bool_set:N = \g__wc_wordcloud_usecolor_bool, |
| 44 |
colors .clist_set:N = \g__wc_wordcloud_colors_clist, % list of LaTeX colors |
| 45 |
colors .value_required:n = true, |
| 46 |
% usecolor .default:n = true, |
| 47 |
} |
| 48 |
|
| 49 |
% local variable to stock metapost colors list |
| 50 |
\tl_new:N \l_colors_list_tl% |
| 51 |
|
| 52 |
% function to build a wordcloud from a list of the form : |
| 53 |
% (word1,weight1);(word2,weight2);etc. |
| 54 |
\NewDocumentCommand\wordcloud{ o m }{% |
| 55 |
% mandatory argument : list (word1,weight1);(word2,weight2);etc. |
| 56 |
% optional arguments : |
| 57 |
% scale=<value> (default 1) |
| 58 |
% rotate=<value> (default 0) |
| 59 |
% margin=<value> (default 0.3pt) |
| 60 |
% usecolor=true/false (default false) |
| 61 |
% colors={color1,color2,etc} |
| 62 |
% pour les clés à valeur par défaut |
| 63 |
\keys_set:nn { wc/wordcloud } { scale=1 } % |
| 64 |
\keys_set:nn { wc/wordcloud } { rotate=0 } % |
| 65 |
\keys_set:nn { wc/wordcloud } { margin=0.3pt } % |
| 66 |
\keys_set:nn { wc/wordcloud } { colors={} } % |
| 67 |
\keys_set:nn { wc/wordcloud } { usecolor=false } % |
| 68 |
|
| 69 |
\tl_clear:N \l_colors_list_tl% |
| 70 |
\IfNoValueF {#1} |
| 71 |
{ |
| 72 |
\keys_set:nn { wc/wordcloud } { #1 } |
| 73 |
} |
| 74 |
|
| 75 |
% parse the list to build a list of rgb colors for metapost code |
| 76 |
|
| 77 |
\clist_map_variable:NNn \g__wc_wordcloud_colors_clist \l_onecolor_tl {% |
| 78 |
\extractcolorspecs{\tl_use:N |
| 79 |
\l_onecolor_tl}{\wc_onecolor_type}{\wc_onecolor_values} |
| 80 |
% get the type and values of color (see xcolor doc) |
| 81 |
\convertcolorspec{\wc_onecolor_type}{\wc_onecolor_values}{rgb}\wc_rgb_values |
| 82 |
% convert to rgb (see xcolor loc) |
| 83 |
\tl_gput_right:Nx \l_colors_list_tl {(\wc_rgb_values);} % add value with |
| 84 |
%parenthesis |
| 85 |
}% |
| 86 |
|
| 87 |
|
| 88 |
%\show {coucous} |
| 89 |
\directlua{% |
| 90 |
%tex.sprint("test") |
| 91 |
wc_build_wordcloud("\luaescapestring{\unexpanded{#2}}",\tl_use:N \g__wc_wordcloud_rotate_tl,\tl_use:N \g__wc_wordcloud_scale_tl,"\tl_use:N \g__wc_wordcloud_margin_tl",\bool_if:NTF \g__wc_wordcloud_usecolor_bool{"true"}{"false"},"\tl_use:N \l_colors_list_tl") |
| 92 |
}% |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
% function to build a wordcloud from a text file : |
| 97 |
\NewDocumentCommand\wordcloudFile{ o m m}{% |
| 98 |
% two mandatory arguments : name of the file and number of words for the |
| 99 |
% wordcloud |
| 100 |
% optional arguments : |
| 101 |
% scale=<value> (default 1) |
| 102 |
% rotate=<value> (default 0) |
| 103 |
% margin=<value> (default 0.3pt) |
| 104 |
% usecolor=true/false (default false) |
| 105 |
% colors={color1,color2,etc} |
| 106 |
|
| 107 |
% pour les clés à valeur par défaut |
| 108 |
\keys_set:nn { wc/wordcloud } { scale=1 } % |
| 109 |
\keys_set:nn { wc/wordcloud } { rotate=0 } % |
| 110 |
\keys_set:nn { wc/wordcloud } { margin=0.3pt } % |
| 111 |
\keys_set:nn { wc/wordcloud } { colors={} } % |
| 112 |
\keys_set:nn { wc/wordcloud } { usecolor=false } % |
| 113 |
\tl_clear:N \l_colors_list_tl% |
| 114 |
|
| 115 |
\IfNoValueF {#1} |
| 116 |
{ |
| 117 |
\keys_set:nn { wc/wordcloud } { #1 } |
| 118 |
} |
| 119 |
|
| 120 |
% parse the list to build a list of rgb colors for metapost code |
| 121 |
|
| 122 |
\clist_map_variable:NNn \g__wc_wordcloud_colors_clist \l_onecolor_tl {% |
| 123 |
\extractcolorspecs{\tl_use:N |
| 124 |
\l_onecolor_tl}{\wc_onecolor_type}{\wc_onecolor_values} |
| 125 |
% get the type and values of color (see xcolor doc) |
| 126 |
\convertcolorspec{\wc_onecolor_type}{\wc_onecolor_values}{rgb}\wc_rgb_values |
| 127 |
% convert to rgb (see xcolor loc) |
| 128 |
\tl_gput_right:Nx \l_colors_list_tl {(\wc_rgb_values);} % add value with |
| 129 |
%parenthesis |
| 130 |
}% |
| 131 |
|
| 132 |
%\show {coucous} |
| 133 |
\directlua{% |
| 134 |
wc_build_wordcloud_file("#2",#3,\tl_use:N \g__wc_wordcloud_rotate_tl,\tl_use:N \g__wc_wordcloud_scale_tl,"\tl_use:N \g__wc_wordcloud_margin_tl",\bool_if:NTF \g__wc_wordcloud_usecolor_bool{"true"}{"false"},"\tl_use:N \l_colors_list_tl") |
| 135 |
}% |
| 136 |
} |
| 137 |
|
| 138 |
% function to add words to ignor when a wordcloud is build from a text file |
| 139 |
\NewDocumentCommand\wordcloudIgnoreWords{m}{% |
| 140 |
% mandatory argument : list of words separated by commas |
| 141 |
% word1, word2, word3, etc. |
| 142 |
\directlua{% |
| 143 |
tab=wc_string_to_tab("#1") |
| 144 |
wc_add_ignored_words(tab) |
| 145 |
}% |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
\ExplSyntaxOff |