无法让 MWE 在 Lyx 中工作

无法让 MWE 在 Lyx 中工作

我是一个新手,已经跳入深渊,试图此 MWE工作。我使用的是 Lyx 2.1.1。

我省略了 MWE 的第一行

\documentclass{article}

第 2 - 28 行我已插入到文档设置下的 LaTex 序言中,即以下代码:

\usepackage{graphicx}
\usepackage{caption}
\usepackage{xcolor}
\usepackage{multicol, lettrine}
\makeatletter
% \begin{macrocode}
\def\csn#1{\csname#1\endcsname}%
\def\ece#1#2{\expandafter#1\csname#2\endcsname}%
\def\setproperty#1#2#3{\ece\edef{#1@p#2}{#3}}%
\def\setpropertyglobal#1#2#3{\ece\xdef{#1@p#2}{#3}}%
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}

% \begin{macro}{\getproperty}
% Used as |\getproperty|\marg{property}\marg{atom}
% \begin{macrocode}
\def\getproperty#1#2{%
  \expandafter\ifx\csname#1@p#2\endcsname\relax
% then \empty
  \else \csname#1@p#2\endcsname
  \fi
}%
% \end{macrocode}
% \end{macro}

我省略了下一行(第 29 行)

\begin{document}

最后使用插入 TeX 代码选项将第 31 行至第 78 行添加到文档本身中:

\def\alist{fig167,fig168,fig169,fig176,%
fig180,fig181,fig182,fig183,fig185,fig186,fig187,fig188}
%% create a macro to create new lists
\def\newDB#1{%
\expandafter\gdef\csname#1\endcsname{}
}
%% add an image to the DB, use the LaTeX macro
%% \g@addto@macro to store them at the DB location
\def\addtoDB#1#2{%
\ifx\@empty#1
\g@addto@macro{#1}{#2}
\else
\g@addto@macro#1{,#2}
\fi
}
%% internal macro, saves the image to the DB
%% #1 is the image database
%% #2 is the image file name
%% #3 is the image long caption.
\def\DB{alist}
\long\def\addimageDB#1#2#3#4{%
%\def\captionname{caption}
% adds to DB #1, the image name #2
\expandafter\addtoDB\expandafter{\csname#1\endcsname}{#2}   
%% set the caption property, set us imagename.caption.data
\setproperty{#2}{caption}{#3}
%% set the date
\setproperty{#2}{date}{#4}
%% makes a label property for use later on fig:name
\setproperty{#2}{label}{fig:#2}
}
%% get the caption of the image
%% #1 database name (not macro)
%% #2 image file
%% sugar for DB terminology
\def\getfield#1#2{%
\getproperty{#1}{#2}
}
% Example code
\addimageDB{alist}{fig145}{This is the caption for image fig145. Testing a paragraph.}{1920}

当我尝试创建 pdf 文档时出现以下错误:

Undefined control sequence

 ...dimageDB{alist}{fig145}{caption145}{1920}

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

我不明白哪里出了问题,因为addimage它是在第 56 行定义的。

答案1

LyX 有时会错误地解析日志文件,或者至少会以一种只能被错误解释的方式显示错误。如果您点击查看完整日志并进行一些滚动,你会发现错误消息类似于

! Undefined control sequence.
\addtoDB ...ty#1 \g @addto@macro{#1}{#2} \else \g 
                                                  @addto@macro#1{,#2} \fi 

换行符之前的最后一个控制序列(在本例中为 )\g是未定义的。该控制序列实际上是\g@addto@macro,但它在 之后停止的原因g@在本例中不被视为正常字母,因此不能成为控制序列名称的一部分。要解决此问题,请\makeatletter在代码块的开头和\makeatother结尾添加 。请参阅\makeatletter 和 \makeatother 起什么作用?(可能还有一些链接的问题)以了解更多信息。基本上,每当您使用@名称中带有 的宏时,都需要\makeatletter在使用前和\makeatother使用后。

我个人认为我会将大多数定义添加到序言中,尽管这不是必需的。/\makeatletter仍然\makeatother是需要的,但由于 LyX 会将它们添加到它生成的 LaTeX 源中,因此您实际上不必自己添加它们。

例如,您可以在序言中添加以下内容:

\usepackage{graphicx}
\usepackage{caption}
\usepackage{xcolor}
\usepackage{multicol, lettrine}

\def\csn#1{\csname#1\endcsname}%
\def\ece#1#2{\expandafter#1\csname#2\endcsname}%
\def\setproperty#1#2#3{\ece\edef{#1@p#2}{#3}}%
\def\setpropertyglobal#1#2#3{\ece\xdef{#1@p#2}{#3}}%
\def\getproperty#1#2{%
  \expandafter\ifx\csname#1@p#2\endcsname\relax
% then \empty
  \else \csname#1@p#2\endcsname
  \fi
}%

\def\alist{fig167,fig168,fig169,fig176,%
fig180,fig181,fig182,fig183,fig185,fig186,fig187,fig188}
%% create a macro to create new lists
\def\newDB#1{%
\expandafter\gdef\csname#1\endcsname{}
}
%% add an image to the DB, use the LaTeX macro
%% \g@addto@macro to store them at the DB location
\def\addtoDB#1#2{%
\ifx\@empty#1
\g@addto@macro{#1}{#2}
\else
\g@addto@macro#1{,#2}
\fi
}
%% internal macro, saves the image to the DB
%% #1 is the image database
%% #2 is the image file name
%% #3 is the image long caption.
\def\DB{alist}
\long\def\addimageDB#1#2#3#4{%
%\def\captionname{caption}
% adds to DB #1, the image name #2
\expandafter\addtoDB\expandafter{\csname#1\endcsname}{#2}   
%% set the caption property, set us imagename.caption.data
\setproperty{#2}{caption}{#3}
%% set the date
\setproperty{#2}{date}{#4}
%% makes a label property for use later on fig:name
\setproperty{#2}{label}{fig:#2}
}
%% get the caption of the image
%% #1 database name (not macro)
%% #2 image file
%% sugar for DB terminology
\def\getfield#1#2{%
\getproperty{#1}{#2}
}

并在文档中添加以下内容的 ERT:

\addimageDB{alist}{fig145}{This is the caption for image fig145. Testing a paragraph.}{1920}

\getfield{fig145}{caption}

然后我得到这个:

在此处输入图片描述

相关内容