从(不完整的)LaTeX 文档中自动提取 Markdown 格式的表格

从(不完整的)LaTeX 文档中自动提取 Markdown 格式的表格

我正在尝试自动呈现我的笔记。我希望能够对我的文档中的所有 Pandoc 表格进行正则表达式处理,在它们周围添加 LaTeX,编译它们并在 PDF 中显示结果。

我想要的自动选择、添加 LaTeX 和编译的流程:

  1. 使用正则表达式选择/提取所有 Pandoc 表(讨论这里关于开发一个)。
  2. 在表格周围添加头部和尾部 LaTeX 代码(关于此主题的讨论已开始这里)。
  3. 编译为 PDF。

我当前对于这个工作流程完全是手动的:

  1. 选择部分文本并粘贴到新文档中;
    • 迭代下一个 Pandoc 表,直到结束。
  2. pandoc -f markdown -t latex在合适的编辑器(例如 Textmate)中根据选择运行。
  3. 手动添加“开始”\usepackage{caption}; \usepackage{longtable}; \usepackage{booktabs}和“结束”\end{document}LaTeX 片段。
  4. 手动按下CMD-R编译文档并查看结果。

此类文件的示例,其中包含 LaTeX 内部 Pandoc 表格的 Markdown 标记以及链接这里

\section{Aortic valve stenosis}
everything - etiology, symptoms, diagnostic tests, grades of aortic valve stenosis

Aortic regurgitation. 
The murmur of aortic regurgitation occurs during diastole as the aortic valve fails to lose completely and blood regurgitates from the aorta back into the LV. 
Murmur - high-pitched decrescendo murmur heard best along left lower sternal border. 

------------------------------------------------------
Age < 70 years (n=324)      Age >70 years (n=322)
--------------------------- --------------------------
Bicuspid (50\%)             Bicuspid (27\%)

Postinflammatory (25\%)     Postinflammatory (23\%)

Degenerative (18\%)         Degenerative (48\%)

Unicommissural (3\%)

Hypoplastic (2\%)           Hypoplastic (2\%)

Indeterminate (2\%)
------------------------------------------------------

Table: Common causes of Aortic Stenosis Among Patients requiring Surgery. 
% http://emedicine.medscape.com/article/150638-overview#aw2aab6b2b4aa


----------------------------------------------------------------------------
                            Aortic      Mild        Moderate    Severe
                            sclerosis   
--------------------------- ----------- ----------- ----------- ------------
Aortic jet velocity (m/s)   ≤2.5 m/s    2.6-2.9     3.0-4.0     >4.0

Mean gradient (mmHg)        -           <20 (<30)   20-40b      >40b (>50a)
                                        (30-50a)    

AVA (cm2)                   -           >1.5        1.0-1.5     <1

Indexed AVA (cm2/m2)                    >0.85       0.60-0.85   <0.6

Velocity ratio                          >0.50       0.25-0.50   <0.25
---------------------------------------------------------------------------


\section{Mitral regurgitation} 
% Etiology, symptoms, diagnostic tests and treatment. 
% Grades of mitral regurgitation. 

Mitral regurgitation (MR) is defined as an abnormal reversal of blood flow from the left ventricle (LV) to the left atrium (LA).
It is caused by disruption in any part of the mitral valve (MV) apparatus. 
The most common etiologies of MR include MV prolapse (MVP), rheumatic heart disease, infective endocarditis, annular calcification, cardiomyopathy, and ischemic heart disease.

---------------------------------
grade           None or trace MR
--------------- -----------------
1               trivial

2               mild

3               moderate

4               severe
----------------------------------

Table: Gradient of colorjet profiles in MR


---------------------------------------------------------------------------
                                Mild        Moderate        Severe
------------------------------- ----------- --------------- --------------- 
Valve area                      > 1.5       1.0-1.5         <1.0

Mean gradent mmHg               <5          5-10            >10

Pulmonary artery pressure mmHg  <30         30-50           >50
---------------------------------------------------------------------------

Table: Classification of mitral stenosis severity. At heart rates between 60-80 bpm in sinus rhythm. 

%% Classification of Mitral valve stenosis
% Wilkins score Grades 1-4. 
% http://www.echopedia.org/wiki/Classification_of_valve_stenosis_and_regurgitation



Mitral regurgitation is a dynamic process; the magnitude of regurgitation differs in early, mid, and late systole.

hp_sep02_valve.pdf

% https://www.youtube.com/watch?v=O0EapRNaNJ0
% hp_sep02_valve.pdf

\subsection{ECHO diagnostics}
Check visual valve assessment by ECHO 

\subsection{Color doppler}


-----------------------------------------------------------------------------
                        Mild                 Moderate         Severe
----------------------- -------------------- ---------------- ---------------                       
Vena contracta (mm)     < 3                  3—6.9            ≥ 7

Jet area (\%)           Small central jet    Variable         Large central 
                        (<20\% of LA area)                    jet (\%gt;40\% 
                                                              of LA area)
-----------------------------------------------------------------------------

Table: Quantification based on Color doppler in Mitral Rerurgitation. 


To examine heart or blood vessels. 
ECHO - high f sound waves. 
Doppler technology allows determination of speed and direction of blood flow by utilizing Doppler effect. 

\subsection{Vena constracta}

Figures. 
12.7.1 vena contracta.png
12.7.1 Jet_Components.png

Vena contracta: The vena contracta corresponds to the region in which blood passes through the valve. Velocity is highest here. The width of the vena contracta is a good marker of the severity of mitral regurgitation because it corresponds to the diameter of the regurgitant orifice area. A diameter exceeding 7 mm indicates severe regurgitation. However, like all distance measurements it is limited by two facts: a) regurgitant orifices may have many geometric shapes, b) quite often more than one jet is present. Nevertheless, the vena contracta is an important clue to the severity of regurgitation.

Vena constructa is the point in a fluid stream where the diameter of the stream is the least, and fluid velocity is at its maximum. 
In ECHO, its measurement describes the smallest area of the blood flow jet as it exits a heart valve. 
This corresponds to the Effective Orifice Area calculated for heart valves using the continuity equation.

With echocardiography it is easy to detect mitral regurgitation. 
In contrast, quantification of mitral regurgitation is much more difficult. 
It calls for considerable experience. 
No single method or feature can be used to fully describe the severity of mitral regurgitation. 
All components of quantification must be considered; an integral approach should be adopted. 
In addition, it is important to understand the hemodynamics and sequelae of mitral regurgitation. 
Put all the pieces of the puzzle together and interpret your findings in the context of clinical findings. 
Ultimately, the severity of mitral regurgitation determines whether one opts for a surgical, an interventional (e.g. the MitraClip procedure) or a conservative approach.




------------------------------------------------------------------------------------
Qualitative (visual assessment)     Semiquantitative         Quantitative
----------------------------------- ------------------------ -----------------------
Jet size (vena contracta,           Size of vena contracta   PISA method
flow convergence), ECHO

LV size (and function),             Size of flow             Volumetric method
LV volume overload?                 convergence zone

PA pressure                         Jet area    

(MV morphology)     

(left atrial size)      

(other indirect signs)      
-----------------------------------------------------------------------------------

Table: Quantification of mitral regurgitation. 
% https://123sonography.com/node/20867




\subsection{PISA}

The PISA (proximal isovelocity surface area) method employs the proximal flow convergence zone to measure the volume of regurgitation

The principle underlying this method is straightforward: the flow convergence zone corresponds to regurgitant flow. Blood flow velocity increases as it approaches the regurgitant orifice. The proximal flow convergence zone can thus be described as "hemispheric shells" in which the velocity on the surface of each of the shells is equal. The quantity of blood flow (regurgitant flow) can be calculated when the radius of the shell and velocity at its surface are known:


Regurgitant flow is 
\begin{equation}
Q = 2 \pi r^{2} v_{Nyquist}
\end{equation}
where $V_{Nyquist}$ is aliasing velocity, and $r$ is PISA radius of convergence zone. 

With the PISA method we use the shell where aliasing occurs - where the color abruptly switches from a distinct blue or red to turbulent (multicolored) flow. Velocity (aliasing velocity) can be determined exactly at this site. We can also measure the radius (r) of the hemisphere at this site.

12.7.3 EROA.png

Calculation of EROA (PISA)
% EROA = Effective Regurgitant Orifice Area. 
% Calculated by instantaneous regurgitant flow and derives from: 
\begin{equation}
EROA = 2 \pi r^2 \frac{V_{alias}}{V_{max}}
\end{equation}
where obtaining the maximum velocity across the MR jet using continuous-wave doppler. 
% http://www.wikiecho.org/wiki/Proximal_isovelocity_surface_area 

Based on the principle of conservation of mass, the PISA method also permits us to measure the effective regurgitant orifice (the functional size of the "hole"). To perform this calculation we have to know the peak velocity of the MR signal. This is achieved by obtaining a CW Doppler spectrum through the mitral regurgitant jet.


------------------------------------------------------------
                    Mild    Moderate            Severe
------------------- ------- ------------------- ------------            
PISA Radius         <0.4                        >1.0
Regurgitant 

volume (ml/beat)    <30     30—44. 45—59        ≥ 60

EROA (cm2)          <0.2    0.2—0.29. 0.3—0.39  ≥ 0.4
------------------------------------------------------------

Table: PISA reference values. 
% https://123sonography.com/node/20867

\subsection{Auscultation}
% since we are talking about valves
hp_sep02_valve.pdf

Diaphragm of stetoscope in supine, left decubitus and sitting positions. 

我如何自动选择所有 Markdown Pandoc 表,然后添加 LaTeX 并运行编译以输出 PDF?

答案1

以下命令可能会让您感到惊讶,因为它在单个管道中实现了您想要的功能(或者至少,它实现了我的想法你要的那个)。

该命令甚至可以放入一条推文中:) :

pandoc test_dummy.tex -f markdown -t html | grep -E '(^<|^$|^ *$)' \
| grep -v "^<p" | pandoc -f html -o tables.pdf

或者,将其格式化得更漂亮一些:

pandoc                   \
     test_dummy.tex      \
    -f markdown          \
    -t html              \
                         \
| grep -E '(^<|^$|^ *$)' \
                         \
| grep -v '^<p'          \
                         \
| pandoc                 \
    -f html              \
    -o tables.pdf        \
    --latex-engine=xelatex

(事实证明,我必须使用它xelatax来生成 PDF——您的源文件包含一些 Unicode 字符,通过默认引擎处理时会触发错误pdflatex。)

解释:

  1. 您的输入文件实际上并不是有效的 LaTeX 文档(尽管.tex文件名中有后缀)。它是 LaTeX 和 Markdown 片段的混合体。

  2. pandoc管道中的第一个命令
    这可能会让您感到惊讶:我声明-f markdown告诉 Pandoc 它应该将输入文件视为 Markdown(而不是 LaTeX!)。

    这个技巧的原因是:Pandoc 容忍raw_texMarkdown 源中散布的片段。当您想要创建基于 LaTeX 的输出文档(Beamer、PDF、LaTeX 本身……)时,这很有用。然后 Pandoc 会让这些原始 TeX 片段不加改变地传递给 LaTeX。——对于非 LaTeX 输出,这些片段会被忽略……因为无论如何您都不关心这些(就您当前的目的而言),我们可以忽略这里出现的任何问题。

    由于该-t html参数,结果输出将具有 HTML 格式。几乎所有内容都将用 HTML 标签标记,除了一些仅由文本字符串组成的行。所有行(除提到的“纯文本字符串”外)都将以 开头<

  3. grep管道中的第一个命令
    这仅保留这些行...

    • ...以标签开头:^<
    • ...或者完全是空的:^$
    • ...或者仅包含空白:^ *$

    因此,没有任何标签的文本片段将

  4. grep管道中的第二条命令:
    这将抛弃所有包含用段落标记包裹的文本并以 开头的行<p

    现在剩下的应该只是用于表格渲染的 HTML 代码。(该 HTML 代码没有开销<html><head> ... </head><body> ... </body></html>)。

  5. pandoc管道中的第二条命令:
    该阶段现在已为另一个 Pandoc 命令做好准备:它将获取代表上一个命令剩余(前 Markdown)表的 HTML 代码,并将其转换为 PDF 文档。

瞧!

以下是结果的截图:

表格.pdf 的截图


注意事项:虽然我的命令适用于您提供的示例文件,但如果您针对其他类似文件运行它,则可能需要进一步调整,这些文件可能包含我可能没有考虑到的其他元素......


更新

为了删除以符号开头的注释掉的行%(按照 OP 在注释中的要求),请使用这个修改后的命令:

pandoc                              \
     test_dummy.tex                 \
    -f markdown                     \
    -t html                         \
                                    \
| grep -E '(^<|^$|^ *$)'            \
                                    \
| grep -v '^<p'                     \
                                    \
| sed 's#%.*</caption>#</caption>#' \
                                    \
| pandoc                            \
    -f html                         \
    -o tables.pdf                   \
    --latex-engine=xelatex

相关内容