1

I have some tex file (e.g. malloc.tex) those contain some sectionss(section of manpages). From there I want to remove the COLOPHON section, because this is not actually man section. And as I merge all tex file in one and make a pdf from this, so there is no need to add COLOPHON section for each one.

enter image description here

File malloc.tex

\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}

I got this tex file by using followings-

Create mancolours.tex containing:

\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}

and then:

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

This question is extended from How to create pdf from Linux man pages so that style is presereved?

EDIT
This can be done using the solution described here. 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
But, I can't make the complex regular expression. I made -

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

that not works.

2
  • Will the COLOPHON always be the last section? That is, ... \section{COLOPHON} ... \end{document}. When you say you want to remove this section but also merge a bunch of TeX files, I assume my question above is not true, correct?
    – Werner
    Commented Dec 30, 2017 at 16:23
  • @Werner As far I know the COLOPHON section is the last section. And, I got some file that have not this section.
    – alhelal
    Commented Dec 30, 2017 at 16:33

1 Answer 1

0

As I know the section content so I can easily remove the section using text manipulating tools like sed, awk, perl, etc.

There are multiple ways -

  • 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

I have got these from terdon

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .