LaTeX中多列文档的\ textwidth吗? [英] \textwidth for multicolumn document in LaTeX?

查看:153
本文介绍了LaTeX中多列文档的\ textwidth吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO Q& A 显示,我发现\ textwidth似乎令人困惑.

As this SO Q & A shows, I found that the \textwidth seems confusing.

通常情况下,这是文本的宽度,但是此变量似乎不具有有关带有multicols的文本宽度的正确信息.

It's the width of text in normal case, but this variable doesn't seem to have the correct info about the width of text with multicols.

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

This is a LaTeX code to print out the width of \textwidth (I copied the code from here).


\documentclass[]{article}
\usepackage{layouts}
\usepackage{multicol}

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

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

\begin{multicols}{2}
textwidth in cm: \printinunitsof{cm}\prntlen{\textwidth}
\end{multicols}

\end{document}

结果,在多列情况下,不可能使用\ textwidth框住文本.

As a result, for multi column case, it's not possible to have a boxed text using \textwidth.


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


\colorbox{red}{\parbox{\textwidth}{Black text on red background}}

  • 如何知道多列文档的实际\ texwidth?
  • \ linewidth有什么问题?我尝试了以下示例,但显示的结果不正确. \ listing环境为我提供了正确的结果.
  • 
    
    \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}{\parbox{\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}
    
    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}
    
    \noindent\colorbox{darkgray}{\parbox{\linewidth}{Black text on red background}}
    
    \noindent\begin{tikzpicture}
      \draw node[draw=black,fill=black!20,rounded corners,inner sep=2ex,text width=\linewidth] {
        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=5pt}
    \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}
    

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

    推荐答案

    看看下面的代码,结果导致方框与列文本的右边距齐平.在multicol环境中,\ linewidth似乎不是文本的宽度.相反,(我认为)列文本的宽度计算为\ linewidth-(\ columnsep/#ofCols).同样在tikz图片中,内部分隔添加到了框的宽度中,因此在设置框的宽度时必须减去它.至少我就是这样:

    Take a look at the code below, which results in boxes flush with right margin of column text. It seems that \linewidth in multicol environment is not the width of the text. Instead, the width of the column text is calculated (I think) as \linewidth - (\columnsep / #ofCols). Also with the tikz picture, the inside separation is added to width of the box, so you have to subtract it when setting the boxes width. At least that's how it seems to me:

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

    这篇关于LaTeX中多列文档的\ textwidth吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆