Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

包含浮动体和 longtable 环境的页面,内容从底部溢出 #40

Closed
annProg opened this issue Jun 3, 2019 · 11 comments
Closed

Comments

@annProg
Copy link

annProg commented Jun 3, 2019

ctexbook文类,没有自动换页,是否是bug?

image

以下是代码

\documentclass[fancyhdr,bookmark]{ctexbook}
\setmainfont{DejaVu Sans} 	% 設定英文字型
\setromanfont{DejaVu Sans} 	% 字型
\setmonofont{DejaVu Sans Mono}

\usepackage{ifxetex,ifluatex}
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

\usepackage{tikz} % Required for drawing custom shapes
\usepackage[yyyymmdd,hhmmss]{datetime}
\ctexset{today=small}

\usepackage{listings}
\newcommand{\passthrough}[1]{\colorbox{lightgray}{#1}}

\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={John Mike},
            pdftitle={PanBook使用手册},
            colorlinks=true,
            urlcolor=blue,
            linkcolor=magenta,
            pdfborder={0 0 0}}
\urlstyle{same}  % don't use monospace font for urls

\usepackage{longtable,booktabs}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother


\title{PanBook使用手册\\\vspace{0.5em}{\large 用Markdown和Pandoc写作}}
\author{John Mike}
\date{\today}

\usepackage{geometry}
\newcommand{\devicepc}{
	\geometry{
		top=1in,
		inner=1in,
		outer=1in,
		bottom=1in,
		headheight=3ex,
		headsep=2ex
	}
}
\devicepc

\definecolor{ocre}{RGB}{243,102,25} % Define the orange color used for highlighting throughout the book
\definecolor{lightgray}{rgb}{0.97,0.97,1}
\renewcommand{\passthrough}[1]{\colorbox{lightgray}{#1}}
\newfontfamily\code{Courier New}

\lstset{
	basicstyle=\small\linespread{1}\code,
	keywordstyle=\color[rgb]{0.13,0.29,0.53}\textbf,
	commentstyle=\color{gray},
	identifierstyle=\color[rgb]{0.00,0.00,0.00},
	stringstyle=\color[rgb]{0.31,0.60,0.02},
	rulesepcolor=\color{red!20!green!20!blue!20},
	backgroundcolor=\color[rgb]{0.97,0.97,0.97},
	tabsize=4,
	breaklines=tr,
	showstringspaces=false,
}
\renewcommand{\lstlistingname}{代码}
\usepackage{keyval}
\newlength\Colsep
\setlength\Colsep{10pt}
\usepackage{fancyhdr}
\pagestyle{fancy}

\begin{document}
\frontmatter
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\mainmatter
    % 在此命令之后的页码为阿拉伯数字
    % 以下为正文
\hypertarget{panbookux624bux518c}{%
\chapter{PanBook手册}\label{panbookux624bux518c}}

\passthrough{\lstinline!PanBook!}是基于\passthrough{\lstinline!Pandoc!}和\passthrough{\lstinline!LaTeX!}开发的一个工具,提供了一些开箱即用的书籍,论文,幻灯片及简历模板,以及一些\passthrough{\lstinline!lua filter!}扩展。用来更方便的使用\passthrough{\lstinline!Pandoc!}将\passthrough{\lstinline!Markdown!}转换为\passthrough{\lstinline!PDF!}或\passthrough{\lstinline!epub!}格式。

\hypertarget{ux5febux901fux5f00ux59cb}{%
\section{快速开始}\label{ux5febux901fux5f00ux59cb}}

以\passthrough{\lstinline!Windows 10!}为例,演示如何使用。

\hypertarget{ux5b89ux88c5ux4f9dux8d56ux8f6fux4ef6}{%
\subsection{安装依赖软件}\label{ux5b89ux88c5ux4f9dux8d56ux8f6fux4ef6}}

\begin{itemize}
\tightlist
\item
  安装\href{https://www.msys2.org/}{msys2}
  (\passthrough{\lstinline!Linux!}及\passthrough{\lstinline!OS X!}请忽略此步骤)
\item
  安装\href{http://mirror.ctan.org/systems/texlive/Images/}{texlive}
  2018或以上版本
\item
  安装\href{https://pandoc.org/installing.html}{Pandoc} 2.7.1或以上版本
\item
  下载\href{https://github.com/lierdakil/pandoc-crossref/releases}{pandoc-crossref}
  对应版本安装到\passthrough{\lstinline!path!}目录下(建议和\passthrough{\lstinline!Pandoc!}放同一目录)
\end{itemize}

\hypertarget{ux5b89ux88c5panbook}{%
\subsubsection{安装PanBook}\label{ux5b89ux88c5panbook}}

\passthrough{\lstinline!PanBook!}是一组\passthrough{\lstinline!Shell!}脚本,直接下载到任意目录即可。打开终端(\passthrough{\lstinline!msys2!}),假设工作目录为\passthrough{\lstinline!/d/!},执行代码~\ref{lst:gitclone}。

\begin{lstlisting}[language=bash, caption=安装PanBook, label=lst:gitclone, float=htbp]
$ cd /d/
$ git clone https://github.com/annProg/PanBook
\end{lstlisting}

\hypertarget{ux8bbeux7f6eux73afux5883ux53d8ux91cf}{%
\subsubsection{设置环境变量}\label{ux8bbeux7f6eux73afux5883ux53d8ux91cf}}

需要将本项目,\passthrough{\lstinline!texlive!}及\passthrough{\lstinline!Pandoc!}加入环境变量,如代码~\ref{lst:setpath}所示。

\begin{lstlisting}[language=bash, caption=设置环境变量, label=lst:setpath, float=htbp]
$ tail -n 1 ~/.bashrc
export PATH=$PATH:/d/texlive/2018/bin/win32:/d/dev/PanBook:/c/Users/myname/AppData/Local/Pandoc
\end{lstlisting}

\hypertarget{ux5f00ux59cbux4f7fux7528}{%
\subsection{开始使用}\label{ux5f00ux59cbux4f7fux7528}}

在任意空目录下执行\passthrough{\lstinline!panbook <command>!},会自动初始化写作环境,生成示例源码。然后在\passthrough{\lstinline!src!}目录下开始写作。\passthrough{\lstinline!<command>!}可以是\passthrough{\lstinline!book!},\passthrough{\lstinline!thesis!},\passthrough{\lstinline!slide!},\passthrough{\lstinline!cv!}。详细帮助信息执行\passthrough{\lstinline!panbook -h!}查看。目录规范见代码~\ref{lst:panbookdirs}。

\begin{lstlisting}[language=bash, caption=目录规范, label=lst:panbookdirs, float=htbp]
.
├── templates                               # 自定义模板
├── styles                                  # 自定义风格
├── extensions                              # 自定义扩展
├── fonts                                   # 自定义字体
├── build                                   # 电子书构建目录
├── src                                     # Markdown源码目录
│   └── images                              # 源码涉及插图目录
│   └── metadata.yaml                       # 书籍元数据文件
│   └── frontmatter.md                      # 前言文件
│   └── backmatter.md                       # 后记文件
│   └── 100-chapter1.md                     # 正文,命名须保证能按正确章节顺序列出
│   └── 200-chapter2.md            
\end{lstlisting}

\hypertarget{sec:note}{%
\subsubsection{注意事项}\label{sec:note}}

\begin{itemize}
\tightlist
\item
  Markdown源码文件需要使用\passthrough{\lstinline!UTF-8!}编码
\item
  Pandoc扩展的Markdown语法要求在标题前留出一个空行,因此按章节拆分的多个Markdown文件,开头需要空一行,否则pandoc不能正确识别标题
\item
  请勿将正文文件命名为
  \passthrough{\lstinline!*frontmatter.md!}或者\passthrough{\lstinline!*backmatter.md!},这2个文件有特殊用途
\end{itemize}

\hypertarget{ux4e66ux7c4dux5143ux6570ux636e}{%
\section{书籍元数据}\label{ux4e66ux7c4dux5143ux6570ux636e}}

在\passthrough{\lstinline!src/metadata.yaml!}中使用\href{http://www.ruanyifeng.com/blog/2016/07/yaml.html}{Yaml语言}
定义书籍的数据及可用的模板变量,示例见代码~\ref{lst:metayaml}。

\begin{lstlisting}[caption=Metadata, label=lst:metayaml, float=htbp]
---
title: PanBook使用手册
subtitle: 用Pandoc和Markdown写作
author:          # 作者(数组)
  - John Mike
homepage: https://github.com/annProg/PanBook
header-includes:
  - \usepackage{cleveref}
  - \usepackage{float}
...
\end{lstlisting}


查看模板文件,可以获取模板支持的所有变量(形如\passthrough{\lstinline!$var$!})。也可以通过修改模板来添加自定义的变量。


\hypertarget{ux5199ux4f5cux5de5ux5177}{%
\section{写作工具}\label{ux5199ux4f5cux5de5ux5177}}

推荐使用 \href{https://code.visualstudio.com/}{Visual Studio
Code}。一些有用的插件见表~\ref{tbl:vscodeplugin}。

\hypertarget{tbl:vscodeplugin}{}
\begin{longtable}[]{@{}lc@{}}
\caption{\label{tbl:vscodeplugin}推荐插件}\tabularnewline
\toprule
插件 & 功能\tabularnewline
\midrule
\endfirsthead
\toprule
插件 & 功能\tabularnewline
\midrule
\endhead
Markdown Preview & Markdown实时预览\tabularnewline
\LaTeX~language support & \LaTeX 语言高亮\tabularnewline
All Autocomplete & 自动补全(支持单词补全)\tabularnewline
\bottomrule
\end{longtable}

使用代码~\ref{lst:vscodeMsys}
将终端改为\passthrough{\lstinline!msys2!}的\passthrough{\lstinline!bash!}。

\begin{lstlisting}[caption={VS Code使用msys2}, label=lst:vscodeMsys, float=htbp]
{
    "terminal.integrated.shell.windows": "D:\\msys64\\usr\\bin\\bash.exe",
    "terminal.integrated.shellArgs.windows": ["-l"],
    "terminal.integrated.env.windows": {
        "CHERE_INVOKING": "1",
        "MSYSTEM": "MINGW64",
    },
    "git.postCommitCommand": "push",
    "git.path": "D:\\msys64\\git-vscode.bat",
    "editor.insertSpaces": false,
    "editor.detectIndentation": false,
}  
\end{lstlisting}

%\begin{help}

通过 VS Code
Tasks,使用\passthrough{\lstinline!PanBook!}部分编译参数\passthrough{\lstinline!--part=md文件名!},可用快捷键(ctrl+shift+b)编译当前Markdown源文件。

通过 VS Code
Tasks,使用\passthrough{\lstinline!PanBook!}部分编译参数\passthrough{\lstinline!--part=md文件名!},可用快捷键(ctrl+shift+b)编译当前Markdown源文件。

通过 VS Code
Tasks,使用\passthrough{\lstinline!PanBook!}部分编译参数\passthrough{\lstinline!--part=md文件名!},可用快捷键(ctrl+shift+b)编译当前Markdown源文件。

通过 VS Code
Tasks,使用\passthrough{\lstinline!PanBook!}部分编译参数\passthrough{\lstinline!--part=md文件名!},可用快捷键(ctrl+shift+b)编译当前Markdown源文件。

通过 VS Code
Tasks,使用\passthrough{\lstinline!PanBook!}部分编译参数\passthrough{\lstinline!--part=md文件名!},可用快捷键(ctrl+shift+b)编译当前Markdown源文件。

通过 VS Code
Tasks,使用\passthrough{\lstinline!PanBook!}部分编译参数\passthrough{\lstinline!--part=md文件名!},可用快捷键(ctrl+shift+b)编译当前Markdown源文件。

通过 VS Code
Tasks,使用\passthrough{\lstinline!PanBook!}部分编译参数\passthrough{\lstinline!--part=md文件名!},可用快捷键(ctrl+shift+b)编译当前Markdown源文件。

通过 VS Code
Tasks,使用\passthrough{\lstinline!PanBook!}部分编译参数\passthrough{\lstinline!--part=md文件名!},可用快捷键(ctrl+shift+b)编译当前Markdown源文件。

通过 VS Code
Tasks,使用\passthrough{\lstinline!PanBook!}部分编译参数\passthrough{\lstinline!--part=md文件名!},可用快捷键(ctrl+shift+b)编译当前Markdown源文件。

通过 VS Code
Tasks,使用\passthrough{\lstinline!PanBook!}部分编译参数\passthrough{\lstinline!--part=md文件名!},可用快捷键(ctrl+shift+b)编译当前Markdown源文件。

%\end{help}

\backmatter

\end{document}
@annProg
Copy link
Author

annProg commented Jun 3, 2019

已解决,去掉 listings中的float=htbp就好了

@annProg annProg closed this as completed Jun 3, 2019
@annProg annProg reopened this Jun 4, 2019
@annProg
Copy link
Author

annProg commented Jun 4, 2019

去掉 float=htbp之后,代码可能跨页,有没有既能避免代码跨页,又能避免分页错误的解决方案?

@muzimuzhi
Copy link
Collaborator

对你提供的例子做了个精简,看起来是 longtable 宏包的 bug。bug 的

  • 复现条件:同一页面的顶部有浮动体,中间有 longtable 环境,环境后有需要跨页的大段文本
  • bug 描述:大段文本中的断页位置,会后移约等于浮动体高度(可通过调整浮动体高度来测试)的位置。

精简后的完整例子

\documentclass{article}
\usepackage{longtable}

% helper pkg
\usepackage[pass, showframe]{geometry}
\usepackage{lipsum}

\begin{document}
\begin{figure}[t]
  a\\b\\c\\d
\end{figure}

\lipsum[1] % optional

\begin{longtable}{l}
  cell
\end{longtable}

\lipsum
\end{document}

输出截图
image

@muzimuzhi
Copy link
Collaborator

muzimuzhi commented Jun 4, 2019

@annProg
经过搜索,这是一个早就(2002 年)发现且有解决方案的 longtable bug。我把相关信息和修复 bug 后的 longtable.sty 放在了这个 gist 里。

@Liam0205 @leo-liu @qinglee
从内容相关性的角度考虑,建议 ctex-kit 的维护者把当前 issue 挪到 https://github.com/CTeX-org/forum 项目中。操作说明见 Transferring an issue to another repository - GitHub Help

@Liam0205 Liam0205 transferred this issue from CTeX-org/ctex-kit Jun 4, 2019
@Liam0205
Copy link
Contributor

Liam0205 commented Jun 4, 2019

@annProg
经过搜索,这是一个早就(2002 年)发现且有解决方案的 longtable bug。我把相关信息和修复 bug 后的 longtable.sty 放在了这个 gist 里。

@Liam0205 @leo-liu @qinglee
从内容相关性的角度考虑,建议 ctex-kit 的维护者把当前 issue 挪到 https://github.com/CTeX-org/forum 项目中。操作说明见 Transferring an issue to another repository - GitHub Help

done

@annProg
Copy link
Author

annProg commented Jun 4, 2019

@muzimuzhi 非常感谢

@muzimuzhi
Copy link
Collaborator

@annProg
根据 latex2e 维护者之一的以下发言,patch 会带来其他问题,所以发布版中一直没有修复。使用时保持谨慎。

可以试试,默认使用 tabular 环境,只对有必要在中间分页的表格(手动)换用 longtable

I seem to recall there was a reason that change wasn't just immediately applied at the time, but I don't see anything in the log, I'd need to investigate, but hopefully this will get you working again.
by David Carlisle
ref: https://tex.stackexchange.com/a/48282/79060

you can find some longtable version 5 trials if you search around my github (folder longtable of repo davidcarlisle/dpctex) but some things get fixed, some things break and backward compatibility is tricky, so things are as they were for now.
by David Carlisle
ref: https://tex.stackexchange.com/questions/48280/longtabu-and-floats-wrong-table-breaks-on-pages-with-floats#comment757361_48282

@muzimuzhi muzimuzhi changed the title 换页错误 包含浮动体和 longtable 环境的页面,内容从底部溢出 Jun 4, 2019
@muzimuzhi
Copy link
Collaborator

(题外话)

可以试试,默认使用 tabular 环境,只对有必要在中间分页的表格(手动)换用 longtable

@annProg 上条建议实践起来可能有一些困难,因为

看起来没有万全之策。在 pandoc 导出的 .tex.pdf 的过程中,哪怕不考虑样式只追求完整输出内容,对特定数据而言,目前也无法避免手动调整。

@annProg
Copy link
Author

annProg commented Jun 4, 2019

@muzimuzhi 是的,目前只能使用longtable。 我在尝试使用gist里的修复方案,以上精简的例子可以修复,但是多几个listings之后又复现了,有办法修复吗?

\documentclass{article}
\usepackage{longtable}

% helper pkg
\usepackage[pass, showframe]{geometry}
\usepackage{lipsum}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[float=htbp]
hello
\end{lstlisting}

\begin{figure}[t]
  a\\b\\c\\d
\end{figure}
\begin{lstlisting}[float=htbp]
hello2
\end{lstlisting}

\lipsum[1] % optional
\begin{lstlisting}[float=htbp]
hello3
\end{lstlisting}

\begin{longtable}{l}
  cell
\end{longtable}
\begin{lstlisting}[float=htbp]
hello4
\end{lstlisting}

\lipsum
\end{document}

@muzimuzhi
Copy link
Collaborator

muzimuzhi commented Jun 4, 2019

我的能力不足,无法修复这个 bug。涉及 longtable 的 bug 还有不少,使用 longtable 而又希望无需人工调整地自动生成 pdf,有相当的风险。

  • 如果大部分表格都是比较短小的,可以用程序替换 tex 文件中的 longtable 环境。
  • 如果接受手动修改,可以根据 log 中的 overfull \vbox (xx.xxpt) 信息,
    • 使用 \enlargethispage{-xx.xxpt} (无法消除日志中的 bad box 信息)
    • longtable 环境下方添加 \global\advance\@colroom-xx.xxpt,见这则回答,在 gist 的 readme 里贴着对应的提问。(能消除日志中的 bad box 信息)

PS:以下面的方式

```latex
<latex code>
```

插入 markdown 中的 fenced code blocks,可以让 <latex code> 部分以 latex language 进行高亮。

@muzimuzhi muzimuzhi reopened this Jun 4, 2019
@annProg
Copy link
Author

annProg commented Jun 5, 2019

@muzimuzhi 谢谢。我尝试一下让pandoc不生成longtable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants