LaTeX 中的多列文档的 \textwidth?

pro*_*eek 5 latex multiple-columns

正如此问答所示,我发现 \textwidth 似乎令人困惑。

它是正常情况下文本的宽度,但此变量似乎没有有关多列文本宽度的正确信息。

这是一个 LaTeX 代码,用于打印 \textwidth 的宽度(我从这里复制了代码)。

\documentclass[]{文章}
\usepackage{布局}
\usepackage{multicol}

\usepackage[左=20mm,右=20mm,上=33.95mm,下=33.95mm]{几何}

\开始{文档}
文本宽度(厘米):\printinunitsof{cm}\prntlen{\textwidth}

\begin{multicols}{2}
文本宽度(厘米):\printinunitsof{cm}\prntlen{\textwidth}
\end{多列}

\结束{文档}

因此,对于多列情况,不可能使用 \textwidth 来显示盒装文本。

\开始{tikz图片}
  \draw 节点[draw=black,fill=black!20,圆角,inner sep=2ex,文字宽度=\textwidth] {
    Lorem ipsum dolor 坐 amet \点
    };
\结束{tikz图片}

或者

\colorbox{red}{\parbox{\textwidth}{红底黑字}}
  • 如何知道多列文档的真实\texwidth?
  • \linewidth 有什么问题?我尝试了以下示例,但结果不正确,如图所示。\listing 环境给了我正确的结果。
\documentclass[]{文章}
\usepackage{布局}
\usepackage{multicol}
\usepackage{tikz}
\usepackage{颜色}
\usepackage{列表}

\usepackage[左=20mm,右=20mm,上=33.95mm,下=33.95mm]{几何}

\开始{文档}
文本宽度(厘米):\printinunitsof{cm}\prntlen{\textwidth}
文本宽度(厘米):\printinunitsof{cm}\prntlen{\linewidth}

\colorbox{darkgray}{红底黑字}

\noindent\colorbox{darkgray}{\parbox{\linewidth}{红底黑字}}

\begin{multicols}{2}


这个很深。还记得etaoin shrdlu吗?(也许你不知道。没关系,关于这个主题的公告即将发布。)还记得光明会和光明会吗?Lorem ipsum 也是同样的情况——那些不断出现的难以理解的短语之一。这肯定意味着什么。当然,它注入了神秘力量。Lorem ipsum,我的孩子们。就这样吧。

文本宽度(厘米):\printinunitsof{cm}\prntlen{\textwidth}
文本宽度(厘米):\printinunitsof{cm}\prntlen{\linewidth}

这个很深。还记得etaoin shrdlu吗?(也许你不知道。没关系,关于这个主题的公告即将发布。)还记得光明会和光明会吗?Lorem ipsum 也是同样的情况——那些不断出现的难以理解的短语之一。这肯定意味着什么。当然,它注入了神秘力量。Lorem ipsum,我的孩子们。就这样吧。


\noindent\colorbox{darkgray}{红底黑字}

\noindent\colorbox{darkgray}{\parbox{\linewidth}{红底黑字}}

\noindent\begin{tikzpicture}
  \draw 节点[draw=black,fill=black!20,圆角,inner sep=2ex,文字宽度=\linewidth] {
    Lorem ipsum dolor 坐 amet \点
    };
\结束{tikz图片}

\definecolor{暗灰色}{rgb}{0.95,0.95,0.95}
\lstset{背景颜色=\color{暗灰色}}
\lstset{columns=fullflexible、basicstyle=\ttfamily、basicstyle=\tiny、numbers=left、numberstyle=\tiny、stepnumber=2、numbersep=5pt}
\begin{lstlisting}[框架=tb]
(让((缓冲区(url同步检索
           “http://tromey.com/elpa/package-install.el”)))
  (保存游览
    (设置缓冲区缓冲区)
    (goto-char (点-分钟))
    (重新向前搜索“^$”nil'移动);;$
    (评估区域(点)(点-masx))
    (终止缓冲区(当前缓冲区))))
\end{列表}

\end{多列}

\结束{文档}

http://img571.imageshack.us/img571/1867/screenshot20100718at407.png

Her*_*itz 2

看一下下面的代码,它会导致框与列文本的右边缘齐平。看来 multicol 环境中的 \linewidth 不是文本的宽度。相反,列文本的宽度计算为(我认为)为 \linewidth - (\columnsep / #ofCols)。另外,对于 tikz 图片,内部分隔会添加到框的宽度中,因此在设置框宽度时必须减去它。至少在我看来是这样的:

\documentclass[]{article}
\usepackage{layouts}
\usepackage{multicol}
\usepackage{tikz}
\usepackage{color}
\usepackage{listings}

\usepackage[left=20mm,right=20mm,top=33.95mm,bottom=33.95mm]{geometry}

\begin{document}
textwidth in cm: \printinunitsof{cm}\prntlen{\textwidth}
textwidth in cm: \printinunitsof{cm}\prntlen{\linewidth}

\colorbox{darkgray}{Black text on red background}

\noindent\colorbox{darkgray}{\makebox[\linewidth]{Black text on red background}}

\begin{multicols}{2}


This one is deep. Remember etaoin shrdlu? (Maybe you don't. Never mind, a bulletin on the subject is forthcoming.) Remember the Illuminati and fnord? Lorem ipsum is the same deal--one of those inscrutable phrases that just keeps turning up. Surely it means something. Surely it's invested with, you know, mysto power. Lorem ipsum, my children. So mote it be.

textwidth in cm: \printinunitsof{cm}\prntlen{\textwidth}
textwidth in cm: \printinunitsof{cm}\prntlen{\linewidth}
\newlength{\mywidth}
\setlength{\mywidth}{\linewidth}
\addtolength{\mywidth}{-0.5\columnsep}
\newlength{\mytikzwidth}
\setlength{\mytikzwidth}{\linewidth}
\addtolength{\mytikzwidth}{-4ex}
mywidth: \the\mywidth\\ columnsep: \the\columnsep\\
\noindent\colorbox{lightgray}{\makebox[\mywidth][c]{Black text on red background}}

This one is deep. Remember etaoin shrdlu? (Maybe you don't. Never mind, a bulletin on the subject is forthcoming.) Remember the Illuminati and fnord? Lorem ipsum is the same deal--one of those inscrutable phrases that just keeps turning up. Surely it means something. Surely it's invested with, you know, mysto power. Lorem ipsum, my children. So mote it be.


\noindent\colorbox{darkgray}{Black text on red background}
mywidth: \the\mywidth columnsep: \the\columnsep colwidth: \the\columnwidth \\

\noindent\colorbox{lightgray}{\makebox[\mywidth][c]{Black text on red background}}

\noindent\begin{tikzpicture}
  \draw node[draw=black,fill=black!20,rounded corners,inner sep=2ex,text width=\mytikzwidth] {
    Lorem ipsum dolor sit amet \dots
    };
\end{tikzpicture}

\definecolor{darkgray}{rgb}{0.95,0.95,0.95}
\lstset{backgroundcolor=\color{darkgray}}
\lstset{columns=fullflexible, basicstyle=\ttfamily,  basicstyle=\tiny, numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=15pt}
\begin{lstlisting}%[frame=tb]
(let ((buffer (url-retrieve-synchronously
           "http://tromey.com/elpa/package-install.el")))
  (save-excursion
    (set-buffer buffer)
    (goto-char (point-min))
    (re-search-forward "^$" nil 'move)  ;;$
    (eval-region (point) (point-masx))
    (kill-buffer (current-buffer))))
\end{lstlisting}

\end{multicols}

\end{document}
Run Code Online (Sandbox Code Playgroud)