从破折号开始水平对齐两行,第一行前一行没有下标,第二行前一行有下标

从破折号开始水平对齐两行,第一行前一行没有下标,第二行前一行有下标

我尝试在 2 个单词后水平对齐 2 行,第一行在文本中不包含下标(参见下面的“WL”),第二行包含下标(参见 GC_{sp})。

我为此尝试了多次,例如\hphantom或“ \,”,但都不起作用。如您所见,前两个左破折号“-”符号之间有一点偏移。

想要从“-”符号对齐

我希望第一行上的第一个左破折号“ -”符号从水平对齐的角度与第二行位于相同的位置,即稍微向右移动。

这里是代码片段:

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} $ }

如果有人能有办法解决这个问题......

编辑1:

和从库中注入到以下结构fisher_1.name中:fisher_2.namecosmicfish

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

我记得我希望第一个“-”符号从水平对齐的角度来看位于相同的水平位置,这意味着从第一个破折号“-”符号到字符串末尾的完美对齐(47.0560.49

最后的解决方案:

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

不起作用。我无法弥补下标引起的差距_{sp}

编辑2:唯一可行的解​​决方案基本上是:

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

这使 :基本解,无下标

使用建议的其他解决方案时,由于我在带有库的代码中定义fisher_1.name并出现了编译错误。fisher_2.namePython[cosmicfish][3]

如果我想要希腊符号 \gamma,这个基本解决方案不再起作用:

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

相同:

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

我很失望,它不适用于您的所有解决方案。我不知道该怎么办……

编辑3:我尝试使用我的 Python 代码中实现的这个示例从头开始重新开始:

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} $'

在执行 Python 代码时,出现以下错误:

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.

编辑4:部分解决方案已完成:

    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}'

快完成了

如您所见,出于我忽略的原因,“WL”字符串被推到了顶部,我不知道为什么。字符串“FoM=47.05”和“FoM=60.49”的字体也很奇怪。

-因此,由于尊重了第一个破折号“ ”的对齐,我们已经接近解决方案。

解决方案 :已被以下人员发现Heiko Theiße

这两行代码起了作用:

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

生成:解决方案

问候

答案1

\hphantom{\mathrm{GC_{sp}}}\llap{$\mathrm{WL}$}在第一行和GC_{sp}第二行中使用。\hphantom产生与一样宽的空间GC_{sp},并将\llap排版$\mathrm{WL}$到该空间中,右对齐。

在第一行使用\rlap{$\mathrm{WL}$}\hphantom{\mathrm{GC_{sp}}}会使其左对齐。

附录:以下工作:

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

挑战在于cosmicfish单独编译行(每行运行一次 LaTeX),因此\halign无法使用表格包。

答案2

你可以通过以下方式将宽度的计算留给 LaTeXeqparbox's \eqmakebox[<tag>][<align>]{<stuff>}。它将<stuff>所有 s 的最大宽度放入一个框内,<tag>并提供单独的 ment 选项<align>left、right 或center)。它需要对最宽元素的宽度进行两次编译,以进行任何更改。

在此处输入图片描述

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

答案3

繁體

Aparbox仅用于第一个“单词”。其宽度设置为固定长度或使用calc包根据最长值计算。

第一种选择更好。你不需要额外的包,而且很容易定义一个 \newlength{<width to fit the longest first word used>}{\mywidth}

然后始终适用\parbox{\mywidth}{<first word>}

不需要额外的编译。

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

相关内容