Changeset 1318 in lmdz_wrf for trunk


Ignore:
Timestamp:
Nov 11, 2016, 4:09:19 PM (8 years ago)
Author:
lfita
Message:

Adding `latex_fig_array': Function to add an array of figures to an existing tex file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1309 r1318  
    6868# index_vec: Function to provide the coordinates of a given value inside a vector
    6969# julday_360d: Function to provide the julian day of a date in a 360 days/yr (or 12 30-days months) calendar
     70# latex_fig_array: Function to add an array of figures to an existing tex file
    7071# latex_text: Function to transform a text to LaTeX following style rules
    7172# list_combos: Function to construct a new list with all possible N-combinations of the list-values
     
    1029610297    return newmat, Lopn[opn]
    1029710298
     10299def latex_fig_array(figs, obf, figcaption, figlabel, dist='sqr', width='auto',       \
     10300  height='None', dorest='nothing'):
     10301    """ Function to add an array of figures to an existing tex file
     10302      figs= list of figures names (with path)
     10303      obf= already exisiting object open tex file
     10304      figcaption= caption for the figure
     10305      figlabel= label for the figure
     10306      dist= kind of distribution
     10307        'sqr': squared, figures are equivenly distributed in rows and columns (sqrt(Nfigs), default)
     10308        [Ncol]'x'Nrow: a given number of columns and rows
     10309      width= with of the figures
     10310        'None': no value passed
     10311        'auto': getting according to fractions of 0.9\textwidth (default)
     10312        [value]: exactly as it is desired (e.g.: '0.3\\textwidht', '5cm' ...)
     10313      height= height of the figures
     10314        'None': no value passed (default)
     10315        'auto': getting according to fractions of 0.9\textheight
     10316        [value]: exactly as it is desired (e.g.: '0.3\\textheight', '5cm' ...)
     10317      dorest= what to do with the last row if it does not fill the number of columns
     10318        'nothing': left empty columns (default)
     10319        'center': center the figures
     10320    >>> latex_fig_array(['figs/figA.png','figs/figB.png','figs/figC.png','figs/figD.png','figs/figE.png'], \
     10321      objf, 'figure test', 'fig:test', dist='2x3', width='None', height='auto', dorest='center')
     10322    \begin{table}
     10323    \begin{center}
     10324    \begin{tabular}{cc}
     10325    \includegraphics[height=0.3\textheight]{figs/figA.png}
     10326    &
     10327    \includegraphics[height=0.3\textheight]{figs/figB.png}
     10328    \\
     10329    \includegraphics[height=0.3\textheight]{figs/figC.png}
     10330    &
     10331    \includegraphics[height=0.3\textheight]{figs/figD.png}
     10332    \\
     10333    \multicolumn{2}{c}{\begin{tabular}{c}
     10334    \includegraphics[height=0.3\textheight]{figs/figE.png}
     10335    \end{tabular}}
     10336    \end{tabular}
     10337    \end{center}
     10338    \caption{figure test}
     10339    \label{fig:test}
     10340    \end{table}
     10341    """
     10342    fname = 'latex_fig_array'
     10343
     10344    Nfig = len(figs)
     10345
     10346    if dist == 'sqr':
     10347        Ncol = int(np.sqrt(Nfig))
     10348        Nrow = Nfig/Ncol
     10349        Nrest = np.mod(Nfig,Ncol)
     10350        if Nrest != 0: Nrow = Nrow + 1
     10351    elif dist.find('x') != -1:
     10352        Ncol = int(dist.split('x')[0])
     10353        Nrow = int(dist.split('x')[1])
     10354        Nrest = np.mod(Nfig,Ncol)
     10355        if Ncol*Nrow < Nfig:
     10356            print errormsg
     10357            print '  ' + fname +': wrong number of rows and columns!!'
     10358            print "  passed value of '" + dist + "' does not fit", Nfig, 'figures'
     10359            quit(-1)
     10360    print '  ' + fname + ': table of figures of: ', Ncol, 'columns', Nrow, 'rows'
     10361
     10362    # Width and height section
     10363    widthheight = ''
     10364    if width != 'None':
     10365        if width == 'auto':
     10366            sec = 0.9/Ncol
     10367            widthheight = 'width=' + str(sec) + '\\textwidth'
     10368        else:
     10369            widthheight = 'width=' + width
     10370
     10371    if height != 'None':
     10372        if len(widthheight) > 0: widthheight = widthheight + ', '
     10373        if height == 'auto':
     10374            sec = 0.9/Nrow
     10375            widthheight = widthheight + 'height=' + str(sec) + '\\textheight'
     10376        else:
     10377            widthheight = widthheight + 'height=' + height
     10378    # num cols
     10379    cS = ''
     10380    for ic in range(Ncol): cS = cS + 'c'
     10381   
     10382    obf.write('\n')
     10383    obf.write('\\begin{table}\n')
     10384    obf.write('\\begin{center}\n')
     10385    obf.write('\\begin{tabular}{' + cS + '}\n')
     10386
     10387    # loop of figures distributed by Ncol, Nrow
     10388    iifig = 0
     10389    for irow in range(Nrow):
     10390        if irow < Nrow -1 or (irow == Nrow - 1 and dorest == 'nothing'):
     10391            for icol in range(Ncol):
     10392                # Getting figure name if it does exist
     10393                if iifig <= Nfig - 1:
     10394                    fign = figs[iifig]
     10395                    obf.write('\\includegraphics[' + widthheight + ']{' +fign+ '}\n')
     10396                    if icol < Ncol - 1:
     10397                        obf.write('&\n')
     10398                    elif irow != Nrow - 1:
     10399                        obf.write('\\\\\n')
     10400                else:
     10401                    if icol < Ncol -2: obf.write('&\n')
     10402                iifig = iifig + 1
     10403        elif irow == Nrow -1 and dorest == 'center':
     10404            crestS = ''
     10405            for irc in range(Nrest): crestS = crestS + 'c'
     10406            obf.write('\\multicolumn{'+str(Ncol)+'}{c}{\\begin{tabular}{'+crestS +   \
     10407              '}\n')
     10408            for icol in range(Nrest):
     10409                fign = figs[iifig]
     10410                print icol, fign, iifig
     10411                obf.write('\\includegraphics[' + widthheight + ']{' + fign + '}\n')
     10412                if icol < Nrest - 1:
     10413                    obf.write('&\n')
     10414                else:
     10415                    obf.write('\\end{tabular}}\n')
     10416                iifig = iifig + 1
     10417
     10418
     10419    obf.write('\\end{tabular}\n')
     10420    obf.write('\\end{center}\n')
     10421    obf.write('\\caption{' + figcaption + '}\n')
     10422    obf.write('\\label{' + figlabel + '}\n')
     10423    obf.write('\\end{table}\n')
     10424    obf.write('\n')
     10425
     10426    return
     10427
    1029810428#quit()
    1029910429
Note: See TracChangeset for help on using the changeset viewer.