Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ซอฟต์แวร์ >> Office

Mastering LaTeX & LyX: เคล็ดลับและเทคนิคจำเป็น

พูด 10 ครั้งติดต่อกัน เร็วๆ ผมท้าคุณ ไม่ว่า จัดตัวกันวันนี้เพื่อเพลิดเพลินกับ Tutorial Ultra-geeky อีกเรื่องเกี่ยวกับว่าเอกสารควรเขียน, สไตล์, จัดรูปแบบ, เตรียม, นำเสนอ และสรุปคือเขียน คุณอาจลured ใช้ Office Suite บางอย่างและ Scribble Work ใน Word Processor แต่ผมขอร้อง อย่าทำ มี Superior Alternatives ทั้งโลกรอคุณ หากคุณยินดี Leap of Faith และ Hurl Into Chasm of Nerdiness

Office Suite Fans จะบอกว่า LaTeX ยากเกินไป และ Good Only สำหรับ Scientific Documents Aha! Well, Kind of True มีหลายสิ่งที่คุณอาจอยากใน LaTeX แตไม่ Instantly Apparent ในบทความนี้ ผมอยากแสดง Common Obstacles ที่ Recent Word User อาจเจอ และวิธี繞過ได้ Quickly และ Painlessly


Grab Your LaTeX

LaTeX คือ One True Way ของการเขียนเอกสาร ใช้ LaTeX จาก Command Line หรือ Frontend ตัวที่สองแนะนำสำหรับคนส่วนใหญ่ Popular Options: LyX และ Kile เช่น LyX Cross-platform รันบน Windows และ Linux ได้ ในบทความนี้ ใช้ LyX เป็น Text Processor คล้ายใช้ Office Suite Program ใดๆ

Note: 所有例子基於 MY Taste 所以確保理解原則 再使用這裡列出的 Tricks 好嗎 讓我們認真 Dive In

Basics

Inserting LaTeX Code 在 LyX Documents ทำได้ด้วย Ctrl + L Permanent Changes ที่ส่งผลทั้ง Document ใส่ใน Document Preamble 一種 Template Section 宣告 Major Stuff: Header, Footer, Bookmarks, Colors, etc.

現在 來檢查 Popular Tricks - 或 Annoyances 如果你願意 我嘗試 Tackle Issues ที่ Normal Person จะ Brave Encounter 當พยายามใช้ LaTeX ตามที่คาดหวังเอกสารพฤติกรรม No Particular Order:

Trick 1: Align Bullets 或 Numbers 到 Left Page Margin

By Default Items Indented by One Tab Space 可能 Annoy You No Worries Solution 極簡單 ก่อน First Itemize 或 Enumerate List ใน Document ต้องเพิ่ม Small Snippet Code หรือใช้ Preamble สำหรับ Bullets และ Numbers ตามลำดับ

\setitemize[0]{leftmargin=XXpt,itemindent=XXpt,labelwidth=XXpt}

\setenumerate[0]{leftmargin=XXpt,itemindent=XXpt,labelwidth=XXpt}

Exact Margins ขึ้นอยู่กับ Needs/Style และ Fonts 但คุณได้ Idea ลองเล่นค่าต่างๆ ดู Final Effect ในกรณีผม ตามภาพด้านล่าง Settings ดังนี้:

\setitemize[0]{leftmargin=15pt,itemindent=0pt,labelwidth=10pt}

ยังต้องใช้ Enumitem Package 宣告ใน Preamble:

\usepackage{enumitem}

ถ้า Package ไม่ Installed ต้อง Install ก่อน ให้ LaTeX Software Handle Installation หรือ Manual Download Missing Packages Extract ไป Right Path 我個人說 别麻烦 让软件做所有硬工夫 例如 Windows, MiKTeX 會做給你:

Trick 2: Nice-looking Footnotes

如果你想要 Footnotes Aligned Nicely 這是 Code 做到 例子包含 Values 我覺得 Work Best 用 Artistic Judgment 調整

\usepackage[hang,splitrule]{footmisc}
\addtolength{\footskip}{0.5cm}
\setlength{\footnotemargin}{0.3cm}
\setlength{\footnotesep}{0.4cm}

Trick 3: Split Long Titles

可能有 Section/Subsection Titles 延伸無盡 使 Table of Contents 看起來 Garbled Solution 簡單 Insert Newline Symbol 在 Title Line ที่จุดที่ต้องการ:

結果:

Trick 4: Image Width

如果需要 Insert Multiple Images 可能想 Scale Graphics ให้ All Look Same 但如果有 Images 不同 Width, Height, Aspect Ratio 你在 Tricky Math 所幸 LyX 讓你 Set Image Width 在 Column 和 Page Width Percentage 使工作簡單得多

Code 為此 使用 100% Column Width 為例:

\begin{figure}[H]
\caption{image title}
\includegraphics[width=1\columnwidth]{image-file-path}
\end{figure}

Caption Optional 當然

Trick 5: Table Cell Padding/Spacing

如果覺得 Default Cell Padding 在 LaTeX 有點 Spartan 和 Ugly 你是對的 Solution: Stretch Table 給 More Space Around Items 使 Less Crowded 和 Easier to Read 例如 Double Cell Spacing:

\renewcommand{\arraystretch}{2}

End Result:

Trick 6: Make a Non-standard Page

如果想 Omit 一個 Page จาก Default Styling? 需要 Special Formatting, Extra Indentation, New Colors, 不管? 為此 使用 Thispagestyle Command 例如 ทำให้ Page 用 No Style:

\thispagestyle{empty}

有用สำหรับ Front Pages ที่ไม่想要 Headers, Footers, Numbering 另一方面 Command ด้านล่าง จะ Specifically Place Headings 到 Page ตาม Definition ใน Preamble

\thispagestyle{headings}

Trick 7: Nice Captions

想要 Captions Fancy, Bold Declaration 和 Nice Wrapping 類似 Footnotes? Sure Do Default Figure Float Captions 有些 Boring 但可以 Pimpify 它們 With Bit of Code:

\usepackage{caption}
\captionsetup{labelfont=bf,format=plain,indention=0cm,
justification=raggedright,singlelinecheck=false}

這進入 Preamble:

需要 Caption Package 才能用 Settings นี้

Trick 8: Insert Programming Language Code with Syntax Highlighting

這是 Real Hardcore 但你會喜歡 而不是 Manual 嘗試 Create Blocks of Text 看起來像 Programming Code 可以用 Listings Package 獲得 Excellent 和 Immediate Results

Listings 提供 Syntax Highlighting 給 Wide Range of Languages, Text Wrapping, Line Numbering, 和其他 Cool Things 不可能解釋 Every Little Detail 但這是 Real-life Case:

\lstset{tabsize=2,breaklines=true,numbers=left,basicstyle=
footnotesize,xleftmargin=30pt}
\lstinputlisting[language=C,]{null-pointer.c}

我們讀 File null-pointer.c 識別為 C Language Default Tab Size 2 Spaces Break Lines 長代碼 Numbers Go Left 30 Points from Page Margin Styled in Footnote Size Simple, Isn't It? Humble Example:

Final Result:

Trick 9: Watermarks

透明 Image Logos 和 Fancy Watermarks? No Problem 也可以做到 Ease 和 Flair 首先 需要 Watermark Package 宣告在 Document Preamble

\usepackage{watermark}

接著 Place Watermark Where Desire 也可以用 \put Command 指定 Exact Location of Image 最佳效果 應該是 Low-contrast, Transparent Image 例如:

\thiswatermark{\centering \put(0,-660){\includegraphics[width=
\textwidth]{tux.png}} }

Command 告訴我們 Place Watermark based on tux.png File, Single Page, Vertical Offset 660pt, Centered to Text Width 可以 Wild with Vertical/Horizontal Positioning, Scaling 什麼也可以 也可以用 Same Watermark All Pages 如果喜歡 我的例子不那麼美 但示範 Concept:

也可以用 Draftcopy Package for DVI/PS Output 或 Pdfdraftcopy for PDF Output 放 Diagonal DRAFT บน Pages 例如:

\usepackage{draftcopy}

Trick 10: Horizontal 和 Vertical Spaces

Normally 應該不做 Styling Changes 到 Pages LaTeX 將 Smartly Parcel Paragraphs/Images Across Pages 試圖 Achieve Best Fit 和 Clarity 同一時間 包括 Slightly Increase/Decrease Line Spacing 或 Shifting Entire Blocks of Text

Still, 如果必須 可以用 \vspace{} 和 \hspace{} Commands 創造 Artificial Gaps 在 Text Flow 當然 應該 Always Check Against Final Output 確保 Look As Expected Exempli Gratia:

\vspace{1cm}

Vertical Spacing 有用如果用 Multiple Columns 想 Different Columns 有 Ragged Bottom Combined with \raggedbottom 和 Clear Page, Vspace 可以 Make Elements Align With Greatest Compactness 尤其有 Images/Tables ที่ Break Flow

Trick 11: Articles with Multiple Columns

說到 Multiple Columns 可以 Create Article with 2, 3 或更多 Columns 相當容易 就用 Multicol Package 宣告在 Preamble 然後 Anywhere 在 Body Switch Between Single/Multiple Columns 如你見 fit:

\begin{multicols}{}
content goes here as usual ...
\end{multicols}

可以 Alternate Between Single 和 Multiple Columns 例如 想 Span Images/Tables Across Page 但用 Two Blocks of Text 記住 Text Span Left to Right Within Block 不會 Break Over Into Second Multi-column Section Further Below

Best Tip of All: Take a Look at the Source

LyX Code 仍然只是 Text 是 LaTeX, 幾個 Special Tags 可以隨時 Open File in Text Editor Examine Special Declarations 你剛做 Using GUI 所以如果 Wonder What Hides Behind Float Elements, Tables 和 Other Cool Formatting 就 Look at the Source Pretty Much Like Examining HTML File

Linux Example:

More Reading

另一個有用的 LaTeX Tips Resource:

LaTeX | Rob Oakes

ข้อสรุป

There You Go 這 Article 是 Thermonuclear Geek Device 但覺得有用 雖然 Personal Experience 可能無用對你 尤其是ไม่เคยลอง LaTeX ก่อน ผมหวัง Article นี้ Intrigue คุณ และ Prompt 你 Trying

Important Lessons Learned Today: LaTeX 可以 Pimp 成 Any Style You Want 有時 Freedom และ Elegance มากกว่า Typical Office Suite Program LaTeX 是 Vast 和 Rich World, 有 Package สำหรับ Pretty Much Anything 可能 Clone Baby Koalas 用 Right Package 它對 Text 是什麼 Repository 對 Linux 最後 你ได้ Simple 和 Quick Answers 給 Common Issues 可能เกิดใน LaTeX Escapades

如果有 Other Cases 你認為 Useful 和 Worthy of Covering, 或 Questions 仍 Lay Around Unanswered 就 Mail Me 可能還有 Sequel

Cheers.