我如何通过名称从 tex 文件中删除某个部分?

我如何通过名称从 tex 文件中删除某个部分?

我有一些 tex 文件(例如malloc.tex),其中包含一些部分(手册页部分)。我想从那里删除该COLOPHON部分,因为这实际上不是手册部分。而且,由于我将所有 tex 文件合并为一个并从中制作 pdf,因此无需COLOPHON为每个文件添加部分。

enter image description here

文件内存分配

\documentclass[]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
    \usepackage{xltxtra,xunicode}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
  \newcommand{\euro}{€}
\fi
% use microtype if available
\IfFileExists{microtype.sty}{\usepackage{microtype}}{}
\usepackage{longtable,booktabs}
\ifxetex
  \usepackage[setpagesize=false, % page size defined by xetex
              unicode=false, % unicode breaks when used with xetex
              xetex]{hyperref}
\else
  \usepackage[unicode=true]{hyperref}
\fi
\hypersetup{breaklinks=true,
            bookmarks=true,
            pdfauthor={},
            pdftitle={MALLOC(3)},
            colorlinks=true,
            citecolor=blue,
            urlcolor=blue,
            linkcolor=magenta,
            pdfborder={0 0 0}}
\urlstyle{same}  % don't use monospace font for urls
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\setcounter{secnumdepth}{0}
\usepackage{pagecolor}

% Set background colour (of the page)
\definecolor{weirdbgcolor}{HTML}{FCF4F0}
\pagecolor{weirdbgcolor}

% Make bold text appear in a particular colour
\definecolor{boldcolor}{HTML}{6E0002}
\let\realtextbf=\textbf
\renewcommand{\textbf}[1]{\textcolor{boldcolor}{\realtextbf{#1}}}

% Use underlines instead of emphasis (ugh)
%\renewcommand{\emph}[1]{\underline{#1}}
\hypersetup{breaklinks=false}

% % Use fixed-width font by default
% \renewcommand*\familydefault{\ttdefault}

\title{MALLOC(3)}
\author{}
\date{}

\begin{document}
\maketitle

\begin{longtable}[c]{@{}lll@{}}
\toprule\addlinespace
MALLOC(3) & Linux Programmer's Manual & MALLOC(3)
\\\addlinespace
\bottomrule
\end{longtable}

\hyperdef{}{NAME}{\section{\hyperref[NAME]{NAME}}\label{NAME}}

malloc, free, calloc, realloc - allocate and free dynamic memory

\hyperdef{}{SYNOPSIS}{\section{\hyperref[SYNOPSIS]{SYNOPSIS}}\label{SYNOPSIS}}

\begin{verbatim}
#include <stdlib.h>
 
void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
\end{verbatim}

\hyperdef{}{DESCRIPTION}{\section{\hyperref[DESCRIPTION]{DESCRIPTION}}\label{DESCRIPTION}}

The \textbf{malloc}() function allocates \emph{size} bytes and returns a
pointer to the allocated memory. \emph{The memory is not initialized}.
If \emph{size} is 0, then \textbf{malloc}() returns either NULL, or a
unique pointer value that can later be successfully passed to
\textbf{free}().

The \textbf{free}() function frees the memory space pointed to by
\emph{ptr}, which must have been returned by a previous call to
\textbf{malloc}(), \textbf{calloc}() or \textbf{realloc}(). Otherwise,
or if \emph{free(ptr)} has already been called before, undefined
behavior occurs. If \emph{ptr} is NULL, no operation is performed.

The \textbf{calloc}() function allocates memory for an array of
\emph{nmemb} elements of \emph{size} bytes each and returns a pointer to
the allocated memory. The memory is set to zero. If \emph{nmemb} or
\emph{size} is 0, then \textbf{calloc}() returns either NULL, or a
unique pointer value that can later be successfully passed to
\textbf{free}().

The \textbf{realloc}() function changes the size of the memory block
pointed to by \emph{ptr} to \emph{size} bytes. The contents will be
unchanged in the range from the start of the region up to the minimum of
the old and new sizes. If the new size is larger than the old size, the
added memory will \emph{not} be initialized. If \emph{ptr} is NULL, then
the call is equivalent to \emph{malloc(size)}, for all values of
\emph{size}; if \emph{size} is equal to zero, and \emph{ptr} is not
NULL, then the call is equivalent to \emph{free(ptr)}. Unless \emph{ptr}
is NULL, it must have been returned by an earlier call to
\textbf{malloc}(), \textbf{calloc}() or \textbf{realloc}(). If the area
pointed to was moved, a \emph{free(ptr)} is done.

\hyperdef{}{RETURNux5fVALUE}{\section{\hyperref[RETURNux5fVALUE]{RETURN
VALUE}}\label{RETURNux5fVALUE}}

The \textbf{malloc}() and \textbf{calloc}() functions return a pointer
to the allocated memory that is suitably aligned for any kind of
variable. On error, these functions return NULL. NULL may also be
returned by a successful call to \textbf{malloc}() with a \emph{size} of
zero, or by a successful call to \textbf{calloc}() with \emph{nmemb} or
\emph{size} equal to zero.

The \textbf{free}() function returns no value.

The \textbf{realloc}() function returns a pointer to the newly allocated
memory, which is suitably aligned for any kind of variable and may be
different from \emph{ptr}, or NULL if the request fails. If \emph{size}
was equal to 0, either NULL or a pointer suitable to be passed to
\textbf{free}() is returned. If \textbf{realloc}() fails the original
block is left untouched; it is not freed or moved.

\hyperdef{}{CONFORMINGux5fTO}{\section{\hyperref[CONFORMINGux5fTO]{CONFORMING
TO}}\label{CONFORMINGux5fTO}}

C89, C99.

\hyperdef{}{NOTES}{\section{\hyperref[NOTES]{NOTES}}\label{NOTES}}

By default, Linux follows an optimistic memory allocation strategy. This
means that when \textbf{malloc}() returns non-NULL there is no guarantee
that the memory really is available. In case it turns out that the
system is out of memory, one or more processes will be killed by the OOM
killer. For more information, see the description of
\emph{/proc/sys/vm/overcommit\_memory} and \emph{/proc/sys/vm/oom\_adj}
in \textbf{proc}(5), and the Linux kernel source file
\emph{Documentation/vm/overcommit-accounting}.

~

Normally, \textbf{malloc}() allocates memory from the heap, and adjusts
the size of the heap as required, using \textbf{sbrk}(2). When
allocating blocks of memory larger than \textbf{MMAP\_THRESHOLD} bytes,
the glibc \textbf{malloc}() implementation allocates the memory as a
private anonymous mapping using \textbf{mmap}(2).
\textbf{MMAP\_THRESHOLD} is 128 kB by default, but is adjustable using
\textbf{mallopt}(3). Allocations performed using \textbf{mmap}(2) are
unaffected by the \textbf{RLIMIT\_DATA} resource limit (see
\textbf{getrlimit}(2)).

~

To avoid corruption in multithreaded applications, mutexes are used
internally to protect the memory-management data structures employed by
these functions. In a multithreaded application in which threads
simultaneously allocate and free memory, there could be contention for
these mutexes. To scalably handle memory allocation in multithreaded
applications, glibc creates additional \emph{memory allocation arenas}
if mutex contention is detected. Each arena is a large region of memory
that is internally allocated by the system (using \textbf{brk}(2) or
\textbf{mmap}(2)), and managed with its own mutexes.

~

The UNIX 98 standard requires \textbf{malloc}(), \textbf{calloc}(), and
\textbf{realloc}() to set \emph{errno} to \textbf{ENOMEM} upon failure.
Glibc assumes that this is done (and the glibc versions of these
routines do this); if you use a private malloc implementation that does
not set \emph{errno}, then certain library routines may fail without
having a reason in \emph{errno}.

Crashes in \textbf{malloc}(), \textbf{calloc}(), \textbf{realloc}(), or
\textbf{free}() are almost always related to heap corruption, such as
overflowing an allocated chunk or freeing the same pointer twice.

Recent versions of Linux libc (later than 5.4.23) and glibc (2.x)
include a \textbf{malloc}() implementation which is tunable via
environment variables. For details, see \textbf{mallopt}(3).

\hyperdef{}{SEEux5fALSO}{\section{\hyperref[SEEux5fALSO]{SEE
ALSO}}\label{SEEux5fALSO}}

\textbf{brk}(2), \textbf{mmap}(2), \textbf{alloca}(3),
\textbf{malloc\_get\_state}(3), \textbf{malloc\_info}(3),
\textbf{malloc\_trim}(3), \textbf{malloc\_usable\_size}(3),
\textbf{mallopt}(3), \textbf{mcheck}(3), \textbf{mtrace}(3),
\textbf{posix\_memalign}(3)

\hyperdef{}{COLOPHON}{\section{\hyperref[COLOPHON]{COLOPHON}}\label{COLOPHON}}

This page is part of release 3.54 of the Linux \emph{man-pages} project.
A description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.

\begin{longtable}[c]{@{}ll@{}}
\toprule\addlinespace
2012-05-10 & GNU
\\\addlinespace
\bottomrule
\end{longtable}

\end{document}

我使用以下命令获取此 tex 文件-

创建mancolours.tex包含:

\usepackage{pagecolor}

% Set background colour (of the page)
\definecolor{weirdbgcolor}{HTML}{FCF4F0}
\pagecolor{weirdbgcolor}

% Make bold text appear in a particular colour
\definecolor{boldcolor}{HTML}{6E0002}
\let\realtextbf=\textbf
\renewcommand{\textbf}[1]{\textcolor{boldcolor}{\realtextbf{#1}}}

% Use underlines instead of emphasis (ugh)
\renewcommand{\emph}[1]{\underline{#1}}

% % Use fixed-width font by default
% \renewcommand*\familydefault{\ttdefault}

进而:

zcat '/usr/share/man/man3/malloc.3.gz' | mandoc -T html > malloc.html
pandoc -s -o malloc.tex --include-in-header=mancolours.tex malloc.html
pdflatex malloc.tex

这个问题延伸自如何从 Linux 手册页创建 pdf 以保留样式?

编辑
这可以使用此处描述的解决方案来完成。https://stackoverflow.com/questions/5178828/how-to-replace-all-lines-between-two-points-and-subtitute-it-with-some-text-in-s?answertab=votes#tab-top

sed -i /BEGIN/,/END/c\ file
但是,我无法创建复杂的正则表达式。我做了 -

 sed -i -e /\\\\hyperdef\{\}\{COLOPHON\}/,/http:\/\/www.kernel.org\/doc\/man-pages\/./c file

那不起作用。

答案1

因为我知道该部分的内容,所以我可以使用文本处理工具(如、、等)轻松删除sed该部分 awkperl

有多种方法 -

  • sed -e '/\\hyperdef{}{COLOPHON}/{ N;N;N;N;d; }' filename
  • sed -e '/\\hyperdef{}{COLOPHON}/,+4d' filename
  • sed '/COLOPHON/,/\/man-pages\/./d' filename
  • perl -0pe 's/\n[^\n]*COLOPHON.*?\n([^\n]*\n){4}/\n/' filename
  • awk '(a>0 || /COLOPHON/){a++} (a==6){a=0} !a' file

我从特登

相关内容