布局和导出 URL

布局和导出 URL

我使用 LaTeX 来编写软件系统测试场景的技术文档。思路如下:

  • 我用 LaTeX 编写了一个测试场景。在这个测试场景中,我使用了可配置的数据片段,这些数据片段是在 LaTeX 环境中定义的。
  • 编译 LaTeX 文档后,我想要一个包含测试描述的漂亮 PDF,以及一个 .properties 文件,该文件仅将所有数据项列为键值对,并直接由测试套件使用。这样,更新测试文档将自动更新可执行测试。

我已经创建了一个环境来实现这一点,如下所示:

\edef\althashchar{\string#\space}%
\newcounter{datasnippets}
\newcommand{\datasnippet}[4]{%
    \immediate\write\tempfile{\unexpanded{#1=#4}}
  \refstepcounter{datasnippets}\label{data:#1}%
  \global\expandafter\def\csname #1\endcsname{#2}%
  \global\expandafter\def\csname #1lc\endcsname{\MakeLowercase{#2}}%    
  \global\expandafter\def\csname #1Description\endcsname{#3}%
  \global\expandafter\def\csname #1Value\endcsname{\code{#4}}%
  \global\expandafter\def\csname #1Full\endcsname{#3 (value: \code{#4})}%
  \thedatasnippets. & #2 & \code{#4} & #3 \\
    \addlinespace
}
\newenvironment{datasnippets}
 {
    \immediate\openout\tempfile=scenarios.properties
    \immediate\write\tempfile{\althashchar This is a generated file, *DO NOT EDIT* directly.}
    \immediate\write\tempfile{\althashchar Instead, update the data snippets inside the data.tex file of the scenarios documentation and regenerate.}

    \par
        \small
        \begin{longtable}{
            p{0.4cm}
            p{4cm}
            p{3cm}
            >{\raggedright\arraybackslash}p{\dimexpr\textwidth-8\tabcolsep-0.4cm-4cm-3cm\relax}
        }
        \toprule
        & \textbf{Name} & \textbf{Value} & \textbf{Description} \\
        \midrule
        \endhead
 }
 {%
    \bottomrule
  \end{longtable}
    \immediate\closeout\tempfile
 }

然后我只需执行以下操作:

\begin{datasnippets}
    \datasnippet{carName}{Car name}{The name (full brand and type) of the car used in the crash-test}{Bugatti Veyron}
\end{datasnippets}

现在,我可以根据我想在文档中使用什么来简单地引用 \carName 、 \carNameValue 、 \carNameDescription ,最后我可以在我的 .properties 文件中很好地导出变量和值。

问题是我无法让它正确处理诸如 URL 之类的东西。这些需要为 LaTeX 进行转义,但它们应该不是在写入文件时被转义。

如何创建值为 URL 的数据片段?理想情况下,我只想执行如下操作:

\begin{datasnippets}
    \datasnippet{shop}{Online store}{The online store used for the order.}{http://www.mycoolstore.com/}
\end{datasnippets}

它应该在 .properties 文件中,如下所示:

shop=http://www.mycoolstore.com/

编辑:

我的问题实际上比 URL 更普遍,它涉及 LaTeX 需要转义的所有内容。例如,如果我有一个形式为 A_B 的变量,我不能简单地在 \datasnippet 命令中输入它,因为我需要转义 _,但输出到属性文件也会导出这些转义字符。因此,如果我有以下代码:

\begin{datasnippets}
    \datasnippet{someCode}{A secret code}{Just some difficult to typeset thing.}{A\_B14879/C\_D}
\end{datasnippets}

将被导出为:

someCode=A\_B14879/C\_D

但我希望看到:

someCode=A_B14879/C_D

这是一个完整的工作示例:

\documentclass[11pt, a4paper]{article}
\usepackage{booktabs}
\usepackage{framed}
\usepackage{longtable}
\usepackage{hyperref}
\usepackage{array}
\usepackage{fancyhdr}
\date{}
\pagestyle{fancy}
\newcommand{\code}[1]{\texttt{#1}}
\newwrite\tempfile


\edef\althashchar{\string#\space}%
\newcounter{datasnippets}
\newcommand{\datasnippet}[4]{%
    \immediate\write\tempfile{\unexpanded{#1=#4}}
  \refstepcounter{datasnippets}\label{data:#1}%
  \global\expandafter\def\csname #1\endcsname{#2}%
  \global\expandafter\def\csname #1lc\endcsname{\MakeLowercase{#2}}%    
  \global\expandafter\def\csname #1Description\endcsname{#3}%
  \global\expandafter\def\csname #1Value\endcsname{\code{#4}}%
  \global\expandafter\def\csname #1Full\endcsname{#3 (waarde: \code{#4})}%
  \thedatasnippets. & #2 & \code{#4} & #3 \\
    \addlinespace
}
\newenvironment{datasnippets}
 {
    \immediate\openout\tempfile=scenarios.properties
    \immediate\write\tempfile{\althashchar This is a generated file, *DO NOT EDIT* directly.}
    \immediate\write\tempfile{\althashchar Instead, update the data snippets inside the data.tex file of the scenarios documentation and regenerate.}

    \par
        \small
        \begin{longtable}{
            p{0.4cm}
            p{4cm}
            p{3cm}
            >{\raggedright\arraybackslash}p{\dimexpr\textwidth-8\tabcolsep-0.4cm-4cm-3cm\relax}
        }
        \toprule
        & \textbf{Name} & \textbf{Value} & \textbf{Description} \\
        \midrule
        \endhead
 }
 {%
    \bottomrule
  \end{longtable}
    \immediate\closeout\tempfile
 }


\begin{document}

\section{Data Overview}
\begin{datasnippets}
% Formaat: naam (dit wordt in het document gebruikt om het commando te gebruiken), korte beschrijving (KORT!), lange beschrijving (duidelijk!), waarde (enkel de waarde, liefst in \code{} wrap)
\datasnippet{someVariable}{Variable}{Just an example}{ThisWorks}
\datasnippet{aProblem}{Problem Variable}{An illustration of a value that layouts fine, but is exported incorrectly.}{A\_B01}
\end{datasnippets}

\section{Data Usage}

As you can see, I can now simply refer to \someVariableValue\ and its full description: \someVariableFull . A side effect is that upon compiling, a properties file is generated which can be used during testing.

\end{document}

有什么建议吗?谢谢!

答案1

以下内容适用于您迄今为止提供的示例:

\documentclass[]{article}

\def\myurl{http://www.my_coolstore.com/}% no escaped underscores
\newwrite\tempfile
\immediate\openout\tempfile=scenarios.properties

\begin{document}
\texttt{\expandafter\detokenize\expandafter{\myurl}}% this prints the url in the pdf
\immediate\write\tempfile{\myurl}% this puts the url in your file
\immediate\closeout\tempfile
\end{document}

编辑:您的 MWE 工作示例,并根据您的 进行了调整\code。您不能使用\_新的 转义下划线 (no ) \code。然后它对我来说很好用。

\documentclass[11pt, a4paper]{article}
\usepackage{booktabs}
\usepackage{framed}
\usepackage{longtable}
\usepackage{hyperref}
\usepackage{array}
\usepackage{fancyhdr}
\date{}
\pagestyle{fancy}
\newcommand{\code}[1]{\texttt{\expandafter\detokenize\expandafter{#1}}}
\newwrite\tempfile


\edef\althashchar{\string#\space}%
\newcounter{datasnippets}
\newcommand{\datasnippet}[4]{%
    \immediate\write\tempfile{\unexpanded{#1=#4}}
  \refstepcounter{datasnippets}\label{data:#1}%
  \global\expandafter\def\csname #1\endcsname{#2}%
  \global\expandafter\def\csname #1lc\endcsname{\MakeLowercase{#2}}%    
  \global\expandafter\def\csname #1Description\endcsname{#3}%
  \global\expandafter\def\csname #1Value\endcsname{\code{#4}}%
  \global\expandafter\def\csname #1Full\endcsname{#3 (waarde: \code{#4})}%
  \thedatasnippets. & #2 & \code{#4} & #3 \\
    \addlinespace
}
\newenvironment{datasnippets}
 {
    \immediate\openout\tempfile=scenarios.properties
    \immediate\write\tempfile{\althashchar This is a generated file, *DO NOT EDIT* directly.}
    \immediate\write\tempfile{\althashchar Instead, update the data snippets inside the data.tex file of the scenarios documentation and regenerate.}

    \par
        \small
        \begin{longtable}{
            p{0.4cm}
            p{4cm}
            p{3cm}
            >{\raggedright\arraybackslash}p{\dimexpr\textwidth-8\tabcolsep-0.4cm-4cm-3cm\relax}
        }
        \toprule
        & \textbf{Name} & \textbf{Value} & \textbf{Description} \\
        \midrule
        \endhead
 }
 {%
    \bottomrule
  \end{longtable}
    \immediate\closeout\tempfile
 }


\begin{document}

\section{Data Overview}
\begin{datasnippets}
% Formaat: naam (dit wordt in het document gebruikt om het commando te gebruiken), korte beschrijving (KORT!), lange beschrijving (duidelijk!), waarde (enkel de waarde, liefst in \code{} wrap)
\datasnippet{someVariable}{Variable}{Just an example}{ThisWorks}
\datasnippet{aProblem}{Problem Variable}{An illustration of a value that layouts fine, but is exported incorrectly.}{A_B01}
\end{datasnippets}

\section{Data Usage}

As you can see, I can now simply refer to \someVariableValue\ and its full description: \someVariableFull . A side effect is that upon compiling, a properties file is generated which can be used during testing.

\end{document}

EDIT2:为了允许 的%参数,我们可以在环境中\datasnippet更改 的 catcode 。这种方式被视为普通字符,这样,环境内部不支持注释。唯一不起作用的是将 用作的参数中的最后一个字符。%datasnippets%\\datasnippet

\documentclass[11pt, a4paper]{article}
\usepackage{booktabs}
\usepackage{framed}
\usepackage{longtable}
\usepackage{hyperref}
\usepackage{array}
\usepackage{fancyhdr}
\date{}
\pagestyle{fancy}
\newcommand{\code}[1]{\texttt{\expandafter\detokenize\expandafter{#1}}}
\newwrite\tempfile


\edef\althashchar{\string#\space}%
\newcounter{datasnippets}
\newcommand{\datasnippet}[4]{%
    \immediate\write\tempfile{\unexpanded{#1=#4}}
  \refstepcounter{datasnippets}\label{data:#1}%
  \global\expandafter\def\csname #1\endcsname{#2}%
  \global\expandafter\def\csname #1lc\endcsname{\MakeLowercase{#2}}%    
  \global\expandafter\def\csname #1Description\endcsname{#3}%
  \global\expandafter\def\csname #1Value\endcsname{\code{#4}}%
  \global\expandafter\def\csname #1Full\endcsname{#3 (waarde: \code{#4})}%
  \thedatasnippets. & #2 & \code{#4} & #3 \\
    \addlinespace
}
\newenvironment{datasnippets}
 {
     \catcode`\%=11
    \immediate\openout\tempfile=scenarios.properties
    \immediate\write\tempfile{\althashchar This is a generated file, *DO NOT EDIT* directly.}
    \immediate\write\tempfile{\althashchar Instead, update the data snippets inside the data.tex file of the scenarios documentation and regenerate.}

    \par
        \small
        \begin{longtable}{
            p{0.4cm}
            p{4cm}
            p{3cm}
            >{\raggedright\arraybackslash}p{\dimexpr\textwidth-8\tabcolsep-0.4cm-4cm-3cm\relax}
        }
        \toprule
        & \textbf{Name} & \textbf{Value} & \textbf{Description} \\
        \midrule
        \endhead
 }
 {%
    \bottomrule
  \end{longtable}
    \immediate\closeout\tempfile
 }


\begin{document}

\section{Data Overview}
% Formaat: naam (dit wordt in het document gebruikt om het commando te gebruiken), korte beschrijving (KORT!), lange beschrijving (duidelijk!), waarde (enkel de waarde, liefst in \code{} wrap)
\begin{datasnippets}
\datasnippet{someVariable}{Variable}{Just an example}{ThisWorks}
\datasnippet{aProblem}{Problem Variable}{An illustration of a value that layouts
fine, but is exported incorrectly.}{A_B01%\ }
\end{datasnippets}
% comment

\section{Data Usage}

As you can see, I can now simply refer to \someVariableValue\ and its full description: \someVariableFull . A side effect is that upon compiling, a properties file is generated which can be used during testing.

\end{document}

相关内容