2

I try to align horizontally 2 lines after 2 words, the first one not containing a subscript in text (see below with "WL") and the second one with a subscript (see GC_{sp}).

I used for this multiple attempts like \hphantom or "\," but none of them is working. As you can see, there is a little shift between the 2 first left dash '-' symbols.

wanting to align from '-' symbol

I want the first left dash '-' symbol on the first line to be at the same place than the second line from an horizontal alignement point of view, i.e to be slightly shifted to the right.

Here the code snippet :

fisher_1.name = '\\parbox{9in}{ $ \\mathrm{WL\\hphantom{ } - Opt. Flat. No \gamma -} \\mathrm{FoM=47.05} $ }'
fisher_2.name = '\\parbox{9in}{ $ \\mathrm{GC_{sp} - Opt. Flat. No \gamma -} \\mathrm{FoM=60.49} $ }

If someone could have the trick to solve this issue...

EDIT1 :

the fisher_1.name and fisher_2.name are injected into the following structure from the library cosmicfish :

fisher_list = fpa.CosmicFish_FisherAnalysis(
    fisher_list=[
        fisher_1,
        fisher_2,
    ],
)

I recall that I would like the first '-' symbol to be at the same horizontal place from an horizontal alignement point of view, which implies a perfect alignement from the first dash '-' symbol up to the end of string ( 47.05 and 60.49.

The last solution :

fisher_1.name = '\\parbox{9in}{$\\mathrm{WL}$\\hphantom{$\\mathrm{_{sp}}$} - FoM=47.05}'
fisher_2.name = '\\parbox{9in}{$\\mathrm{GC_{sp}}$ - FoM=60.49}'

doesn't work. I can't compensate the gap provocated by subscript _{sp}

EDIT 2: The only solution that works is basically :

fisher_1.name = 'WL   - Opt. Flat. No gamma - FoM=47.05'
fisher_2.name = 'GCsp - Opt. Flat. No gamma - FoM=60.49'

which gives : basic solution but without subscript

With the other solutions suggested, I have compilation errors since I define fisher_1.name and fisher_2.name in a Python code with the library [cosmicfish][3].

If I want the greek symbol \gamma, this basic solution doesn't work anymore :

fisher_1.name = 'WL   - Opt. Flat. No $\gamma$ - FoM=47.05'
fisher_2.name = 'GCsp - Opt. Flat. No $\gamma$ - FoM=60.49'

the same for :

fisher_1.name = 'WL   - Opt. Flat. No $\\gamma$ - FoM=47.05'
fisher_2.name = 'GCsp - Opt. Flat. No $\\gamma$ - FoM=60.49'

I am disapointed that it doesn't work with all your solutions. I don't know what to do...

EDIT 3: I try to restart from the beginning with this example implemented in my Python code :

fisher_1.name = '\\parbox{\\widthof{$\\mathrm{GC_{sp}}$}}{$\\mathrm{WL}$}  $\\mathrm{-\\ Opt.\\ Flat.\\ No\\ \\gamma - FoM=47.05} $'
fisher_2.name = '\\parbox{\\widthof{$\\mathrm{GC_{sp}}$}}{$\\mathrm{GC_{sp}}$}  $\\mathrm{-\\ Opt.\\ Flat.\\ No\\ \\gamma - FoM=60.49} $'

At the execution of Python code, I get the following errors :

TITLE - name of file : \parbox{\widthof{$\mathrm{GC_{sp}}$}}{$\mathrm{WL}$}  $\mathrm{-\ Opt.\ Flat.\ No\ \gamma - FoM=47.05} $
Traceback (most recent call last):
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/texmanager.py", line 254, in _run_checked_subprocess
    stderr=subprocess.STDOUT)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/subprocess.py", line 411, in check_output
    **kwargs).stdout
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/subprocess.py", line 512, in run
    output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['latex', '-interaction=nonstopmode', '--halt-on-error', '/Users/fab/.matplotlib/tex.cache/e8dd1b2348d85fb40145a12d56a0f625.tex']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "main_synthesis.py", line 904, in <module>
    plot_tri(fisher_plotter)
  File "main_synthesis.py", line 206, in plot_tri
    set_triplot_dimensions( self, num_col=len(params_temp), num_rows=len(params_temp), **kwargs )
  File "main_synthesis.py", line 673, in set_triplot_dimensions
    dimensions  = self.get_dimensions_plot_obj()
  File "cosmicfish-pyplots-py3/cosmicfish_pylib/fisher_plot.py", line 778, in get_dimensions_plot_obj
    self.figure.canvas.draw()
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 406, in draw
    self.figure.draw(self.renderer)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/artist.py", line 74, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/figure.py", line 2791, in draw
    renderer, self, artists, self.suppressComposite)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/legend.py", line 609, in draw
    bbox = self._legend_box.get_window_extent(renderer)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 352, in get_window_extent
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 439, in get_extent_offsets
    for c in self.get_visible_children()]
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 439, in <listcomp>
    for c in self.get_visible_children()]
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 347, in get_extent
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 475, in get_extent_offsets
    for c in self.get_visible_children()]
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 475, in <listcomp>
    for c in self.get_visible_children()]
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 347, in get_extent
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 439, in get_extent_offsets
    for c in self.get_visible_children()]
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 439, in <listcomp>
    for c in self.get_visible_children()]
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 347, in get_extent
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 475, in get_extent_offsets
    for c in self.get_visible_children()]
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 475, in <listcomp>
    for c in self.get_visible_children()]
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/offsetbox.py", line 823, in get_extent
    bbox, info, yd = self._text._get_layout(renderer)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/text.py", line 315, in _get_layout
    clean_line, self._fontproperties, ismath=ismath)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py", line 230, in get_text_width_height_descent
    s, fontsize, renderer=self)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/texmanager.py", line 399, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/texmanager.py", line 293, in make_dvi
    texfile], tex, cwd=tmpdir)
  File "/opt/intel/oneapi/intelpython/latest/lib/python3.7/site-packages/matplotlib/texmanager.py", line 267, in _run_checked_subprocess
    exc=exc.output.decode('utf-8'))) from exc
RuntimeError: latex was not able to process the following string:
b'$\\\\mathrm{\\\\parbox{\\\\widthof{$\\\\mathrm{GC_{sp}}$}}{$\\\\mathrm{WL}$}\\\\ \\\\ $\\\\mathrm{-\\\\\\\\ Opt.\\\\\\\\ Flat.\\\\\\\\ No\\\\\\\\ \\\\gamma\\\\ -\\\\ FoM=47.05}\\\\ $}$'

Here is the full report generated by latex:
This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020/MacPorts 2020.54632_1) (preloaded format=latex)
 restricted \write18 enabled.
entering extended mode
(/Users/fab/.matplotlib/tex.cache/e8dd1b2348d85fb40145a12d56a0f625.tex
LaTeX2e <2020-02-02> patch level 5
L3 programming layer <2020-03-06>
(/opt/local/share/texmf-texlive/tex/latex/base/article.cls
Document Class: article 2019/12/20 v1.4l Standard LaTeX document class
(/opt/local/share/texmf-texlive/tex/latex/base/size10.clo))
(/opt/local/share/texmf-texlive/tex/latex/type1cm/type1cm.sty)
(/opt/local/share/texmf-texlive/tex/latex/cm-super/type1ec.sty
(/opt/local/share/texmf-texlive/tex/latex/base/t1cmr.fd))
(/opt/local/share/texmf-texlive/tex/latex/base/inputenc.sty)
(/opt/local/share/texmf-texlive/tex/latex/geometry/geometry.sty
(/opt/local/share/texmf-texlive/tex/latex/graphics/keyval.sty)
(/opt/local/share/texmf-texlive/tex/generic/iftex/ifvtex.sty
(/opt/local/share/texmf-texlive/tex/generic/iftex/iftex.sty)))
(/opt/local/share/texmf-texlive/tex/latex/base/textcomp.sty)
(/opt/local/share/texmf-texlive/tex/latex/l3backend/l3backend-dvips.def)
No file e8dd1b2348d85fb40145a12d56a0f625.aux.
*geometry* driver: auto-detecting
*geometry* detected driver: dvips
! Undefined control sequence.
<argument> \widthof
                    {$\mathrm {GC_{sp}}$}
l.19 ....\\ Flat.\\ No\\ \gamma\ -\ FoM=47.05}\ $}
                                                  $}
No pages of output.
Transcript written on e8dd1b2348d85fb40145a12d56a0f625.log.

EDIT 4: A partial solution is done :

    fisher_1.name = '\\parbox{9in}{\\rlap{$\\mathrm{WL}$}\\hphantom{$\\mathrm{GC_{sp}}$} - FoM=47.05}'
    fisher_2.name = '\\parbox{9in}{$\\mathrm{GC_{sp}}$ - FoM=60.49}'

almost done

As you can see, and for a reason I ignore, the "WL" string is pushed to the top, I don't why. The font for strings "FoM=47.05" and "FoM=60.49" is also weird.

So, we are near to a solution since the alignement regarding first dash '-' is respected.

SOLUTION : has been found by Heiko Theiße :

these 2 lines did the trick :

   fisher_1.name = '\\rlap{$\\mathrm{WL}$\\hphantom{\\mathrm{GC_{sp}}} - FoM=47.05'
   fisher_2.name = 'GC_{sp} - FoM=60.49' 

which produces : solution

Regards

17
  • 2
    Why can't you try in tabular format?
    – MadyYuvi
    Commented Oct 29, 2021 at 7:43
  • @MadyYuvi . Thanks for your quick answer, could you tell me please how to use tabular in my case ? I inject fisher_1.name and fisher_2.name after in a legend.
    – user94782
    Commented Oct 29, 2021 at 8:14
  • I'm not sure what the color bar means, I've just tried with the remaining text \documentclass{book} \usepackage{tabularx} \begin{document} \begin{tabularx}{\textwidth}{lcp{5cm}} $ \mathrm{WL}$ &$-$ &$\mathrm{Opt. Flat. No \gamma - FoM=47.05} $\\ $ \mathrm{GC_{sp}}$ &$-$ &$\mathrm{Opt. Flat. No \gamma - FoM=60.49}$ \end{tabularx} \end{document}
    – MadyYuvi
    Commented Oct 29, 2021 at 8:35
  • An easier option is the tabto package. Please see the manual.
    – Fran
    Commented Oct 29, 2021 at 8:40
  • 1
    It would be probably easier to come up with a solution if we knew the context of this. Where are you going to paste these two lines of LaTeX into and in which way? Commented Oct 31, 2021 at 16:08

3 Answers 3

3
+50

Use \hphantom{\mathrm{GC_{sp}}}\llap{$\mathrm{WL}$} in the first row and GC_{sp} in the second. The \hphantom produces a space that is as wide as GC_{sp}, and the \llap typesets $\mathrm{WL}$ into that space, right-aligned.

Using \rlap{$\mathrm{WL}$}\hphantom{\mathrm{GC_{sp}}} in the first row instead will make it left-aligned.

Addendum: The following works:

fisher_1.name = '\\rlap{$\\mathrm{WL}$}\\hphantom{\\mathrm{GC_{sp}}} - FoM=47.05'
fisher_2.name = 'GC_{sp} - FoM=60.49'

The challenge is that cosmicfish compiles the rows separately (one LaTeX run per row), therefore \halign or a table package cannot be used.

7
  • Thanks for your anwser. Unfortunately, I get error with the following string : fisher_1.name = '\\parbox{9in}{$\\hphantom{\\mathrm{GC_{sp}}}\\llap{\\mathrm{WL}}$ - FoM=47.05}' fisher_2.name = '\\parbox{9in}{$\\mathrm{GC_{sp}}$ - FoM=60.49}' I get the error : ! LaTeX Error: \mathrm allowed only in math mode. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.19 ..._{sp}}}\llap{\mathrm{WL}}$\ -\ FoM=47.05}}
    – user94782
    Commented Oct 31, 2021 at 15:26
  • 1
    You should write \parbox{9in}{\hphantom{$\mathrm{GC_{sp}}$}\llap{$\mathrm{WL}$} - FoM=47.05} (with all the backslashes doubled in your case). The macros \hphantom and \llap should not be inside the math mode part. Commented Oct 31, 2021 at 16:12
  • @JasperHabicht . Sorry, this try doesn't work : fisher_1.name = '\\parbox{9in}{\\hphantom{$\\mathrm{GC_{sp}}$}\\llap{$\\mathrm{WL}$} - FoM=47.05}' fisher_2.name = '\\parbox{9in}{$\\\mathrm{GC_{sp}}$ - FoM=60.49}' I get the following legend : i.imgur.com/Hjpkaak.png
    – user94782
    Commented Oct 31, 2021 at 16:31
  • You entered a third backslash: $\\\mathrm{GC_{sp}}$ should be $\\mathrm{GC_{sp}}$ of course. Commented Oct 31, 2021 at 16:33
  • 1
    @JasperHabicht : the third backslash removed, I still have a shift from the first ledt dash ''-'' symbol between the 2 lines.
    – user94782
    Commented Nov 1, 2021 at 14:26
1

You can leave the calculation of widths up to LaTeX via eqparbox's \eqmakebox[<tag>][<align>]{<stuff>}. It puts <stuff> inside a box of maximum width across all <tag>s with the option of individual <align>ment as well (left, right or center). It requires two compilations with any change in the width of the widest element.

enter image description here

\documentclass{article}

\usepackage{eqparbox}

\setlength{\parindent}{0pt}% Just for this example

\begin{document}

\eqmakebox[LHS][l]{WL} -- Opt.\ Flat.\ No $\gamma$ - FoM = 47.05

\eqmakebox[LHS][l]{GC\textsubscript{sp}} -- Opt.\ Flat.\ No $\gamma$ - FoM = 60.49 

\end{document}
2
  • Thanks for your solution ! isn't there a way to avoid 2 compilations ?
    – user94782
    Commented Nov 2, 2021 at 16:31
  • @youpilat13: There's only 2 compilations required if there's a change in the widest element. So, upon first use, there would be 2 compilations. But thereafter only 1 (you don't have to compile twice every time). The first compilation writes the widest length to the .aux. The second compilation sets everything to that widest length with the same <tag>. If the widest element doesn't change width, then there's no need for a second compilation. It's convenient because you let (La)TeX find the width without having to specify the widest component explicitly (like in the other solutions).
    – Werner
    Commented Nov 2, 2021 at 18:19
1

bx

A parbox is used only for the first "word". Its width is set to a fixed length or calculated from the longest using the calc package.

The first option is better. You don't need additional packages and it's easy to define a \newlength{<width to fit the longest first word used>}{\mywidth}

and then always apply \parbox{\mywidth}{<first word>}.

No additional compilations are required.

\documentclass{article}

\usepackage{calc} % \widthof{..}

\begin{document}
    
    \textbf{Original}\medskip
        
    \parbox{9in}{ $ \mathrm{WL\hphantom{ } - Opt. Flat. No \gamma -} \mathrm{FoM=47.05} $ }
    
    \parbox{9in}{ $ \mathrm{GC_{sp} - Opt. Flat. No \gamma -} \mathrm{FoM=60.49} $ }    
        
    \bigskip
    \textbf{A fixed length}\medskip
        
    \parbox{5ex}{$\mathrm{WL}$}  $\mathrm{-\ Opt.\ Flat.\ No\ \gamma - FoM=47.05} $
    
    \parbox{5ex}{$\mathrm{GC_{sp}}$}   $\mathrm{-\ Opt.\ Flat.\ No\ \gamma - FoM=60.49} $ 
    
    \bigskip
    \textbf{Using calc}\medskip 
    
    \parbox{\widthof{$\mathrm{GC_{sp}}$}}{$\mathrm{WL}$}  $\mathrm{-\ Opt.\ Flat.\ No\ \gamma - FoM=47.05} $
    
    \parbox{\widthof{$\mathrm{GC_{sp}}$}}{$\mathrm{GC_{sp}}$}  $\mathrm{-\ Opt.\ Flat.\ No\ \gamma - FoM=60.49} $ 

\end{document}

You must log in to answer this question.