我怎样才能改进这个 LaTeX 代码?

我怎样才能改进这个 LaTeX 代码?

我有一份包含英语短语的文档。重读单词用粗体显示,上方有一个较大的圆点。非重读单词上方有一个较小的圆点。

在此处输入图片描述

我喜欢当前的布局。这是我使用的 LaTeX 代码:

\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage{fontspec}
\usepackage{geometry}
\usepackage{enumitem}

\setmainfont{Charis SIL}

\geometry{a4paper, left=1in, right=1in, top=0.5in, bottom=0.65in}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1em}
\renewcommand{\baselinestretch}{1.4}

\usepackage{tikz}         

\usepackage{stackengine}
\renewcommand\stacktype{L}

\newcommand{\lstress}[1]
{%
    \addstackgap[10pt]{\stackon{#1}{%
            \begin{tikzpicture}
                \fill[opacity=0] circle (0.5ex);
                \fill circle (0.25ex);
            \end{tikzpicture}
        }%
    }%
}%

\newcommand{\hstress}[1]
{%
    \addstackgap[10pt]{\stackon{\bfseries{#1}}{\tikz \fill circle (0.5ex);}}%
}%

\begin{document}
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    
    \begin{enumerate}   
        \item \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word}
        
        \par\addvspace{-0.5ex}{phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription}\par\addvspace{3ex}
        
        \item \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word}
        
        \par\addvspace{-0.5ex}{phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription}\par\addvspace{3ex}
        
        \item \hstress{What} \hstress{kind} \lstress{of} \hstress{books} \lstress{do} \lstress{you} \hstress{like} \lstress{to} \hstress{read}?
        \par\addvspace{-0.5ex}{phonetic transcription}\par\addvspace{3ex}
    \end{enumerate}

    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
\end{document}

第一个短语较长,分为两行。

请注意,我在音标中的每个段落中重复了\par\addvspace{-0.5ex}和。是否可以创建一个宏来自动执行此操作?也许通过将短语包装成类似的东西?\par\addvspace{3ex}newcommand\phrase{.....}

答案1

例如,你的 LaTeX 代码应该像这样改进:

\documentclass[12pt,a4paper]{article}

\def\baselinestretch {1.4}

\def\lstress#1{\leavevmode\vbox{\halign{\hfil##\hfil\cr\cr$\ssize\bullet$\cr#1\cr}}}
\def\hstress#1{\leavevmode\vbox{\halign{\hfil##\hfil\cr\cr$\bullet$\cr\bf#1\cr}}}
\let\ssize=\scriptscriptstyle

\def\replspace #1 #2{\space #1-%
   \ifx#2\end \else \expandafter\replspace\expandafter#2\fi}
\def\metrix #1#2{\leavevmode \def\tmpa{#1}%
   \edef\tmpb{\replspace #2 \end}\expandafter\meA\tmpb\end}
\def\meA #1#2-{\meB{#1#2}\isnextchar{ }{ \meA}{\isnextchar\end{\meE}{\meA}}}
\def\meB#1{\expandafter\nextA\tmpa\relax\end
   \ifx\next*\hstress{#1}\fi
   \ifx\next.\lstress{#1}\fi
}
\def\nextA #1#2\end{\let\next=#1\def\tmpa{#2}}
\def\meE{\let\next=}

\def\isnextchar#1#2#3{\begingroup\toks0={\endgroup#2}\toks1={\endgroup#3}%
   \let\tmp= #1\futurelet\next\isnextcharA
}
\def\isnextcharA{\the\toks\ifx\tmp\next0\else1\fi\space}

\def\phrase#1#2#3#4{\metrix{#1}{#2}#3\vskip-0.5ex\relax #4\vskip3ex\relax}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

\begin{enumerate}   
    \item \phrase
    { .    *    .    *    .    *    .    *    .    *    .    *    .    *    .    *    .    *    .    *  }
    {Word Word Word Word Word Word Word Word Word Word Word Word Word Word Word Word Word Word Word Word}.
    {phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription}

    \item \phrase
    { .    *    .    *    .    *    .    *    .    *  }
    {Word Word Word Word Word Word Word Word Word Word}.
    {phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription}

    \item \phrase
    { *    *   .   *     .  .   *    .  *  }
    {What kind of books do you like to read}?
    {phonetic transcription}

    \item \phrase
    {.   *     .    *    .  }
    {A-longer-word Word Word}.
    {}
\end{enumerate}

\end{document}

注意,我没有使用任何 LaTeX 软件包。也许,如果你需要使用Charis SIL(我没有这个字体),那么fontspec当然可以使用。

答案2

首先,将你的布局放入一个带有两个参数的宏中:

\xitem{<words>}{<phonetic transcription>}

或者在普通文本中使用类似的

\transc{<words>}{<phonetic transcription>}

enumerateitemize

A

在普通文本中

b

\documentclass[12pt,a4paper]{article}

%\usepackage[utf8]{inputenc}% not needed anymore
\usepackage{fontspec}
\usepackage{geometry}
\usepackage{enumitem}

\setmainfont{Charis SIL}

\geometry{a4paper, left=1in, right=1in, top=0.5in, bottom=0.65in}
\setlength{\parindent}{0pt}
\setlength{\parskip}{1em}
\renewcommand{\baselinestretch}{1.4}

\usepackage{tikz}         

\usepackage{stackengine}
\renewcommand\stacktype{L}

\newcommand{\lstress}[1]
{%
    \addstackgap[10pt]{\stackon{#1}{%
            \begin{tikzpicture}
                \fill[opacity=0] circle (0.5ex);
                \fill circle (0.25ex);
            \end{tikzpicture}
        }%
    }%
}%

\newcommand{\hstress}[1]
{%
\addstackgap[10pt]{\stackon{\bfseries{#1}}{\tikz \fill circle (0.5ex);}}%
}%

\newcommand{\xitem}[2]{% added <<<<<<<<<<<
    \item #1\par\addvspace{-0.5ex}#2\par\addvspace{3ex}
}

\newcommand{\transc}[2]{%to use in normal text added <<<<<<<<<<<
 #1\par\addvspace{-0.5ex}#2\par\addvspace{3ex}
}

\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

\begin{enumerate}   
    \item \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word}
    
    \par\addvspace{-0.5ex}{phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription}\par\addvspace{3ex}
    
    \item \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word}
    
    \par\addvspace{-0.5ex}{phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription}\par\addvspace{3ex}
    
    \item \hstress{What} \hstress{kind} \lstress{of} \hstress{books} \lstress{do} \lstress{you} \hstress{like} \lstress{to} \hstress{read}?
    \par\addvspace{-0.5ex}{phonetic transcription}\par\addvspace{3ex}
\end{enumerate}

Using \verb|\xitem|


\begin{enumerate}  
    \xitem{\lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word}}{phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription}

    \xitem{\hstress{What} \hstress{kind} \lstress{of} \hstress{books} \lstress{do} \lstress{you} \hstress{like} \lstress{to} \hstress{read}?}{phonetic transcription}
    
\end{enumerate} 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

\newpage

In a normal text  use the similar \verb|\transc|

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.


\transc{\lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word}}{phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription}

\transc{\lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word} \lstress{Word} \hstress{Word}}{phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription phonetic transcription}

\transc{\hstress{What} \hstress{kind} \lstress{of} \hstress{books} \lstress{do} \lstress{you} \hstress{like} \lstress{to} \hstress{read}?}{phonetic transcription}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

\end{document}

相关内容