Class: ReVIEW::LATEXBuilder

Inherits:
Builder show all
Includes:
LaTeXUtils, TextUtils
Defined in:
lib/review/latexbuilder.rb

Constant Summary collapse

HEADLINE =
{
  1 => 'chapter',
  2 => 'section',
  3 => 'subsection',
  4 => 'subsubsection',
  5 => 'paragraph',
  6 => 'subparagraph'
}.freeze
BOUTEN =
'・'.freeze

Constants inherited from Builder

Builder::CAPTION_TITLES

Instance Attribute Summary

Attributes inherited from Builder

#doc_status

Instance Method Summary collapse

Methods included from TextUtils

#add_space?, #defer_math_image, #detab, #join_lines_to_paragraph, #split_paragraph

Methods included from LaTeXUtils

#escape, #escape_index, #escape_url, #initialize_metachars, #macro, #unescape

Methods inherited from Builder

#bibpaper, #bind, #compile_inline, #detab, #embed, #error, #escape, #extract_chapter_id, #firstlinenum, #get_chap, #graph, #graph_aafigure, #graph_blockdiag, #graph_gnuplot, #graph_graphviz, #graph_plantuml, #image, #initialize, #inline_column, #inline_embed, #inline_hd, #inline_href, #inline_imgref, #inline_include, #inline_kw, #inline_ruby, #inline_w, #inline_wb, #line_num, #load_words, #over_secnolevel?, #parse_table_rows, #post_paragraph, #pre_paragraph, #raw, #system_graph, #table_row_separator_regexp, #target_name, #text, #tsize, #ul_item_begin, #ul_item_end, #warn

Constructor Details

This class inherits a constructor from ReVIEW::Builder

Instance Method Details

#bibpaper_bibpaper(_id, _caption, lines) ⇒ Object



1241
1242
1243
1244
1245
1246
1247
1248
1249
# File 'lib/review/latexbuilder.rb', line 1241

def bibpaper_bibpaper(_id, _caption, lines)
  if @book.config['join_lines_by_lang']
    print split_paragraph(lines).join("\n\n")
  else
    print split_paragraph(lines).map(&:chomp).join("\n")
  end

  puts ''
end

#bibpaper_header(id, caption) ⇒ Object



1236
1237
1238
1239
# File 'lib/review/latexbuilder.rb', line 1236

def bibpaper_header(id, caption)
  puts "[#{@chapter.bibpaper(id).number}] #{compile_inline(caption)}"
  puts macro('label', bib_label(id))
end

#blanklineObject



925
926
927
# File 'lib/review/latexbuilder.rb', line 925

def blankline
  puts '\vspace*{\baselineskip}'
end

#box(lines, caption = nil) ⇒ Object



242
243
244
245
246
247
248
249
250
251
# File 'lib/review/latexbuilder.rb', line 242

def box(lines, caption = nil)
  blank
  puts macro('reviewboxcaption', compile_inline(caption)) if caption.present?
  puts '\begin{reviewbox}'
  lines.each do |line|
    puts detab(line)
  end
  puts '\end{reviewbox}'
  blank
end

#captionblock(type, lines, caption) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/review/latexbuilder.rb', line 215

def captionblock(type, lines, caption)
  if @book.config.check_version('2', exception: false)
    type = 'minicolumn'
  end

  print "\\begin{review#{type}}"

  @doc_status[:caption] = true
  if @book.config.check_version('2', exception: false)
    puts
    if caption.present?
      puts "\\reviewminicolumntitle{#{compile_inline(caption)}}"
    end
  else
    if caption.present?
      print "[#{compile_inline(caption)}]"
    end
    puts
  end

  @doc_status[:caption] = nil
  blocked_lines = split_paragraph(lines)
  puts blocked_lines.join("\n\n")

  puts "\\end{review#{type}}"
end

#center(lines) ⇒ Object Also known as: centering



851
852
853
# File 'lib/review/latexbuilder.rb', line 851

def center(lines)
  latex_block('center', lines)
end

#cmd(lines, caption = nil, lang = nil) ⇒ Object



395
396
397
398
399
400
401
402
# File 'lib/review/latexbuilder.rb', line 395

def cmd(lines, caption = nil, lang = nil)
  if highlight_listings?
    common_code_block_lst(nil, lines, 'reviewcmdlst', 'title', caption, lang)
  else
    blank
    common_code_block(nil, lines, 'reviewcmd', caption, lang) { |line, idx| code_line('cmd', line, idx, nil, caption, lang) }
  end
end

#code_line(_type, line, _idx, _id, _caption, _lang) ⇒ Object



349
350
351
# File 'lib/review/latexbuilder.rb', line 349

def code_line(_type, line, _idx, _id, _caption, _lang)
  detab(line) + "\n"
end

#code_line_num(_type, line, first_line_num, idx, _id, _caption, _lang) ⇒ Object



353
354
355
# File 'lib/review/latexbuilder.rb', line 353

def code_line_num(_type, line, first_line_num, idx, _id, _caption, _lang)
  detab((idx + first_line_num).to_s.rjust(2) + ': ' + line) + "\n"
end

#column_begin(level, label, caption) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/review/latexbuilder.rb', line 181

def column_begin(level, label, caption)
  blank
  @doc_status[:column] = true

  target = nil
  if label
    target = "\\hypertarget{#{column_label(label)}}{}"
  else
    target = "\\hypertarget{#{column_label(caption)}}{}"
  end

  @doc_status[:caption] = true
  if @book.config.check_version('2', exception: false)
    puts '\\begin{reviewcolumn}'
    puts target
    puts macro('reviewcolumnhead', nil, compile_inline(caption))
  else
    # ver.3
    print '\\begin{reviewcolumn}'
    puts "[#{compile_inline(caption)}#{target}]"
  end
  @doc_status[:caption] = nil

  if level <= @book.config['toclevel'].to_i
    puts "\\addcontentsline{toc}{#{HEADLINE[level]}}{#{compile_inline(caption)}}"
  end
end

#column_end(_level) ⇒ Object



209
210
211
212
213
# File 'lib/review/latexbuilder.rb', line 209

def column_end(_level)
  puts '\\end{reviewcolumn}'
  blank
  @doc_status[:column] = nil
end

#comment(lines, comment = nil) ⇒ Object



903
904
905
906
907
908
909
910
911
# File 'lib/review/latexbuilder.rb', line 903

def comment(lines, comment = nil)
  return true unless @book.config['draft']
  lines ||= []
  unless comment.blank?
    lines.unshift(escape(comment))
  end
  str = lines.join('\par ')
  puts macro('pdfcomment', str)
end

#common_code_block(id, lines, command, caption, _lang) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/review/latexbuilder.rb', line 404

def common_code_block(id, lines, command, caption, _lang)
  @doc_status[:caption] = true
  unless @book.config.check_version('2', exception: false)
    puts '\\begin{reviewlistblock}'
  end
  if caption.present?
    if command =~ /emlist/ || command =~ /cmd/ || command =~ /source/
      puts macro(command + 'caption', compile_inline(caption))
    else
      begin
        if get_chap.nil?
          puts macro('reviewlistcaption', "#{I18n.t('list')}#{I18n.t('format_number_header_without_chapter', [@chapter.list(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}")
        else
          puts macro('reviewlistcaption', "#{I18n.t('list')}#{I18n.t('format_number_header', [get_chap, @chapter.list(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}")
        end
      rescue KeyError
        error "no such list: #{id}"
      end
    end
  end
  @doc_status[:caption] = nil
  body = ''
  lines.each_with_index do |line, idx|
    body.concat(yield(line, idx))
  end
  puts macro('begin', command)
  print body
  puts macro('end', command)
  unless @book.config.check_version('2', exception: false)
    puts '\\end{reviewlistblock}'
  end
  blank
end

#common_code_block_lst(_id, lines, command, title, caption, lang, first_line_num: 1) ⇒ Object



438
439
440
441
442
443
444
445
446
447
448
# File 'lib/review/latexbuilder.rb', line 438

def common_code_block_lst(_id, lines, command, title, caption, lang, first_line_num: 1)
  if title == 'title' && caption.blank? && @book.config.check_version('2', exception: false)
    print '\vspace{-1.5em}'
  end
  body = lines.inject('') { |i, j| i + detab(j) + "\n" }
  args = make_code_block_args(title, caption, lang, first_line_num: first_line_num)
  puts %Q(\\begin{#{command}}[#{args}])
  print body
  puts %Q(\\end{#{command}})
  blank
end

#compile_href(url, label) ⇒ Object



1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
# File 'lib/review/latexbuilder.rb', line 1283

def compile_href(url, label)
  if /\A[a-z]+:/ =~ url
    if label
      macro('href', escape_url(url), escape(label))
    else
      macro('url', escape_url(url))
    end
  else
    macro('ref', url)
  end
end

#compile_kw(word, alt) ⇒ Object



1275
1276
1277
1278
1279
1280
1281
# File 'lib/review/latexbuilder.rb', line 1275

def compile_kw(word, alt)
  if alt
    macro('reviewkw', escape(word)) + "(#{escape(alt.strip)})"
  else
    macro('reviewkw', escape(word))
  end
end

#compile_ruby(base, ruby) ⇒ Object



1045
1046
1047
# File 'lib/review/latexbuilder.rb', line 1045

def compile_ruby(base, ruby)
  macro('ruby', escape(base), escape(ruby).gsub('\\textbar{}', '|'))
end

#dd(lines) ⇒ Object



306
307
308
309
310
311
312
# File 'lib/review/latexbuilder.rb', line 306

def dd(lines)
  if @book.config['join_lines_by_lang']
    puts join_lines_to_paragraph(lines)
  else
    puts lines.map(&:chomp).join("\n")
  end
end

#direct(lines, fmt) ⇒ Object



896
897
898
899
900
901
# File 'lib/review/latexbuilder.rb', line 896

def direct(lines, fmt)
  return unless fmt == 'latex'
  lines.each do |line|
    puts line
  end
end

#dl_beginObject



296
297
298
299
# File 'lib/review/latexbuilder.rb', line 296

def dl_begin
  blank
  puts '\begin{description}'
end

#dl_endObject



314
315
316
317
# File 'lib/review/latexbuilder.rb', line 314

def dl_end
  puts '\end{description}'
  blank
end

#dt(str) ⇒ Object



301
302
303
304
# File 'lib/review/latexbuilder.rb', line 301

def dt(str)
  str = str.gsub('[', '\lbrack{}').gsub(']', '\rbrack{}')
  puts '\item[' + str + '] \mbox{} \\\\'
end

#emlist(lines, caption = nil, lang = nil) ⇒ Object



357
358
359
360
361
362
363
364
# File 'lib/review/latexbuilder.rb', line 357

def emlist(lines, caption = nil, lang = nil)
  blank
  if highlight_listings?
    common_code_block_lst(nil, lines, 'reviewemlistlst', 'title', caption, lang)
  else
    common_code_block(nil, lines, 'reviewemlist', caption, lang) { |line, idx| code_line('emlist', line, idx, nil, caption, lang) }
  end
end

#emlistnum(lines, caption = nil, lang = nil) ⇒ Object



366
367
368
369
370
371
372
373
374
# File 'lib/review/latexbuilder.rb', line 366

def emlistnum(lines, caption = nil, lang = nil)
  blank
  first_line_num = line_num
  if highlight_listings?
    common_code_block_lst(nil, lines, 'reviewemlistnumlst', 'title', caption, lang, first_line_num: first_line_num)
  else
    common_code_block(nil, lines, 'reviewemlist', caption, lang) { |line, idx| code_line_num('emlistnum', line, first_line_num, idx, nil, caption, lang) }
  end
end

#emtable(lines, caption = nil) ⇒ Object



799
800
801
# File 'lib/review/latexbuilder.rb', line 799

def emtable(lines, caption = nil)
  table(lines, nil, caption)
end

#extnameObject



26
27
28
# File 'lib/review/latexbuilder.rb', line 26

def extname
  '.tex'
end

#flushright(lines) ⇒ Object



857
858
859
# File 'lib/review/latexbuilder.rb', line 857

def flushright(lines)
  latex_block('flushright', lines)
end

#footnote(id, content) ⇒ Object



1017
1018
1019
1020
1021
1022
1023
1024
# File 'lib/review/latexbuilder.rb', line 1017

def footnote(id, content)
  if @book.config['footnotetext'] || @foottext[id]
    if @doc_status[:column]
      warn "//footnote[#{id}] is in the column block. It is recommended to move out of the column block."
    end
    puts macro("footnotetext[#{@chapter.footnote(id).number}]", compile_inline(content.strip))
  end
end

#handle_metric(str) ⇒ Object



492
493
494
495
496
497
# File 'lib/review/latexbuilder.rb', line 492

def handle_metric(str)
  if @book.config['pdfmaker']['image_scale2width'] && str =~ /\Ascale=([\d.]+)\Z/
    return "width=#{$1}\\maxwidth"
  end
  str
end

#headline(level, label, caption) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/review/latexbuilder.rb', line 115

def headline(level, label, caption)
  _, anchor = headline_prefix(level)
  headline_name = HEADLINE[level]
  if @chapter.is_a?(ReVIEW::Book::Part)
    if @book.config.check_version('2', exception: false)
      headline_name = 'part'
    elsif level == 1
      headline_name = 'part'
      puts '\begin{reviewpart}'
    end
  end
  prefix = ''
  if level > @book.config['secnolevel'] || (@chapter.number.to_s.empty? && level > 1)
    prefix = '*'
  end
  blank unless @output.pos == 0
  @doc_status[:caption] = true
  puts macro(headline_name + prefix, compile_inline(caption))
  @doc_status[:caption] = nil
  if prefix == '*' && level <= @book.config['toclevel'].to_i
    puts "\\addcontentsline{toc}{#{headline_name}}{#{compile_inline(caption)}}"
  end
  if level == 1
    puts macro('label', chapter_label)
  else
    puts macro('label', sec_label(anchor))
    puts macro('label', label) if label
  end
rescue
  error "unknown level: #{level}"
end

#highlight?Boolean

Returns:

  • (Boolean)


339
340
341
342
# File 'lib/review/latexbuilder.rb', line 339

def highlight?
  @book.config['highlight'] &&
    @book.config['highlight']['latex']
end

#hrObject



913
914
915
# File 'lib/review/latexbuilder.rb', line 913

def hr
  puts '\hrule'
end

#image_dummy(id, caption, lines) ⇒ Object



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/review/latexbuilder.rb', line 530

def image_dummy(id, caption, lines)
  warn "image not bound: #{id}"
  puts '\begin{reviewdummyimage}'
  # path = @chapter.image(id).path
  puts "--[[path = #{id} (#{existence(id)})]]--"
  lines.each do |line|
    puts detab(line.rstrip)
  end
  puts macro('label', image_label(id))
  @doc_status[:caption] = true
  if @book.config.check_version('2', exception: false)
    puts macro('caption', compile_inline(caption)) if caption.present?
  else
    puts macro('reviewimagecaption', compile_inline(caption)) if caption.present?
  end
  @doc_status[:caption] = nil
  puts '\end{reviewdummyimage}'
end

#image_extObject



1299
1300
1301
# File 'lib/review/latexbuilder.rb', line 1299

def image_ext
  'pdf'
end

#image_header(id, caption) ⇒ Object



481
482
# File 'lib/review/latexbuilder.rb', line 481

def image_header(id, caption)
end

#image_image(id, caption, metric) ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/review/latexbuilder.rb', line 503

def image_image(id, caption, metric)
  metrics = parse_metric('latex', metric)
  # image is always bound here
  puts "\\begin{reviewimage}%%#{id}"

  command = 'reviewincludegraphics'
  if @book.config.check_version('2', exception: false)
    command = 'includegraphics'
  end

  if metrics.present?
    puts "\\#{command}[#{metrics}]{#{@chapter.image(id).path}}"
  else
    puts "\\#{command}[width=\\maxwidth]{#{@chapter.image(id).path}}"
  end
  @doc_status[:caption] = true

  if @book.config.check_version('2', exception: false)
    puts macro('caption', compile_inline(caption)) if caption.present?
  else
    puts macro('reviewimagecaption', compile_inline(caption)) if caption.present?
  end
  @doc_status[:caption] = nil
  puts macro('label', image_label(id))
  puts '\end{reviewimage}'
end

#imgtable(lines, id, caption = nil, metric = nil) ⇒ Object



803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# File 'lib/review/latexbuilder.rb', line 803

def imgtable(lines, id, caption = nil, metric = nil)
  unless @chapter.image_bound?(id)
    warn "image not bound: #{id}"
    image_dummy(id, caption, lines)
    return
  end

  begin
    if caption.present?
      puts "\\begin{table}[h]%%#{id}"
      @doc_status[:caption] = true
      puts macro('reviewimgtablecaption', compile_inline(caption))
      @doc_status[:caption] = nil
    end
    puts macro('label', table_label(id))
  rescue ReVIEW::KeyError
    error "no such table: #{id}"
  end
  imgtable_image(id, caption, metric)

  if caption.present?
    puts '\end{table}'
  end
  blank
end

#imgtable_image(id, _caption, metric) ⇒ Object



829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/review/latexbuilder.rb', line 829

def imgtable_image(id, _caption, metric)
  metrics = parse_metric('latex', metric)
  # image is always bound here
  puts "\\begin{reviewimage}%%#{id}"

  command = 'reviewincludegraphics'
  if @book.config.check_version('2', exception: false)
    command = 'includegraphics'
  end

  if metrics.present?
    puts "\\#{command}[#{metrics}]{#{@chapter.image(id).path}}"
  else
    puts "\\#{command}[width=\\maxwidth]{#{@chapter.image(id).path}}"
  end
  puts '\end{reviewimage}'
end

#indepimage(lines, id, caption = nil, metric = nil) ⇒ Object Also known as: numberlessimage



588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/review/latexbuilder.rb', line 588

def indepimage(lines, id, caption = nil, metric = nil)
  metrics = parse_metric('latex', metric)

  if @chapter.image(id).path
    puts "\\begin{reviewimage}%%#{id}"

    command = 'reviewincludegraphics'
    if @book.config.check_version('2', exception: false)
      command = 'includegraphics'
    end

    if metrics.present?
      puts "\\#{command}[#{metrics}]{#{@chapter.image(id).path}}"
    else
      puts "\\#{command}[width=\\maxwidth]{#{@chapter.image(id).path}}"
    end
  else
    warn "image not bound: #{id}"
    puts '\begin{reviewdummyimage}'
    puts "--[[path = #{id} (#{existence(id)})]]--"
    lines.each do |line|
      puts detab(line.rstrip)
    end
  end

  @doc_status[:caption] = true
  if caption.present?
    puts macro('reviewindepimagecaption',
               %Q(#{I18n.t('numberless_image')}#{I18n.t('caption_prefix')}#{compile_inline(caption)}))
  end
  @doc_status[:caption] = nil

  if @chapter.image(id).path
    puts '\end{reviewimage}'
  else
    puts '\end{reviewdummyimage}'
  end
end

#index(str) ⇒ Object



1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
# File 'lib/review/latexbuilder.rb', line 1251

def index(str)
  sa = str.split('<<>>')

  sa.map! do |item|
    if @index_db[item]
      escape_index(escape(@index_db[item])) + '@' + escape_index(escape(item))
    else
      if item =~ /\A[[:ascii:]]+\Z/ || @index_mecab.nil?
        esc_item = escape_index(escape(item))
        if esc_item != item
          "#{escape_index(item)}@#{esc_item}"
        else
          esc_item
        end
      else
        yomi = NKF.nkf('-w --hiragana', @index_mecab.parse(item).force_encoding('UTF-8').chomp)
        escape_index(escape(yomi)) + '@' + escape_index(escape(item))
      end
    end
  end

  "\\index{#{sa.join('!')}}"
end

#inline_ami(str) ⇒ Object



1193
1194
1195
# File 'lib/review/latexbuilder.rb', line 1193

def inline_ami(str)
  macro('reviewami', escape(str))
end

#inline_b(str) ⇒ Object

bold



1083
1084
1085
1086
1087
1088
1089
# File 'lib/review/latexbuilder.rb', line 1083

def inline_b(str)
  if @book.config.check_version('2', exception: false)
    macro('textbf', escape(str))
  else
    macro('reviewbold', escape(str))
  end
end

#inline_balloon(str) ⇒ Object



1232
1233
1234
# File 'lib/review/latexbuilder.rb', line 1232

def inline_balloon(str)
  macro('reviewballoon', escape(str))
end

#inline_bib(id) ⇒ Object



1142
1143
1144
# File 'lib/review/latexbuilder.rb', line 1142

def inline_bib(id)
  macro('reviewbibref', "[#{@chapter.bibpaper(id).number}]", bib_label(id))
end

#inline_bou(str) ⇒ Object



1041
1042
1043
# File 'lib/review/latexbuilder.rb', line 1041

def inline_bou(str)
  macro('reviewbou', escape(str))
end

#inline_br(_str) ⇒ Object

line break



1092
1093
1094
# File 'lib/review/latexbuilder.rb', line 1092

def inline_br(_str)
  "\\\\\n"
end

#inline_chap(id) ⇒ Object



945
946
947
948
949
950
951
952
953
954
# File 'lib/review/latexbuilder.rb', line 945

def inline_chap(id)
  if @book.config['chapterlink']
    "\\hyperref[chap:#{id}]{#{@book.chapter_index.number(id)}}"
  else
    @book.chapter_index.number(id)
  end
rescue KeyError
  error "unknown chapter: #{id}"
  nofunc_text("[UnknownChapter:#{id}]")
end

#inline_chapref(id) ⇒ Object



933
934
935
936
937
938
939
940
941
942
943
# File 'lib/review/latexbuilder.rb', line 933

def inline_chapref(id)
  title = super
  if @book.config['chapterlink']
    "\\hyperref[chap:#{id}]{#{title}}"
  else
    title
  end
rescue KeyError
  error "unknown chapter: #{id}"
  nofunc_text("[UnknownChapter:#{id}]")
end

#inline_code(str) ⇒ Object

@<code> is same as @<tt>



1102
1103
1104
1105
1106
1107
1108
# File 'lib/review/latexbuilder.rb', line 1102

def inline_code(str)
  if @book.config.check_version('2', exception: false)
    macro('texttt', escape(str))
  else
    macro('reviewcode', escape(str))
  end
end

#inline_column_chap(chapter, id) ⇒ Object



1161
1162
1163
1164
1165
1166
1167
# File 'lib/review/latexbuilder.rb', line 1161

def inline_column_chap(chapter, id)
  macro('reviewcolumnref',
        I18n.t('column', compile_inline(chapter.column(id).caption)),
        column_label(id, chapter))
rescue KeyError
  error "unknown column: #{id}"
end

#inline_comment(str) ⇒ Object



1220
1221
1222
1223
1224
1225
1226
# File 'lib/review/latexbuilder.rb', line 1220

def inline_comment(str)
  if @book.config['draft']
    macro('pdfcomment', escape(str))
  else
    ''
  end
end

#inline_del(str) ⇒ Object



1122
1123
1124
# File 'lib/review/latexbuilder.rb', line 1122

def inline_del(str)
  macro('reviewstrike', escape(str))
end

#inline_dtp(_str) ⇒ Object



1096
1097
1098
1099
# File 'lib/review/latexbuilder.rb', line 1096

def inline_dtp(_str)
  # ignore
  ''
end

#inline_em(str) ⇒ Object



1181
1182
1183
# File 'lib/review/latexbuilder.rb', line 1181

def inline_em(str)
  macro('reviewem', escape(str))
end

#inline_eq(id) ⇒ Object



1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
# File 'lib/review/latexbuilder.rb', line 1006

def inline_eq(id)
  chapter, id = extract_chapter_id(id)
  if get_chap(chapter).nil?
    macro('reviewequationref', I18n.t('format_number_without_chapter', [chapter.equation(id).number]))
  else
    macro('reviewequationref', I18n.t('format_number', [get_chap(chapter), chapter.equation(id).number]))
  end
rescue KeyError
  error "unknown equation: #{id}"
end

#inline_fn(id) ⇒ Object



1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
# File 'lib/review/latexbuilder.rb', line 1026

def inline_fn(id)
  if @book.config['footnotetext']
    macro("footnotemark[#{@chapter.footnote(id).number}]", '')
  elsif @doc_status[:caption] || @doc_status[:table] || @doc_status[:column] || @doc_status[:dt]
    @foottext[id] = @chapter.footnote(id).number
    macro('protect\\footnotemark', '')
  else
    macro('footnote', compile_inline(@chapter.footnote(id).content.strip))
  end
rescue KeyError
  error "unknown footnote: #{id}"
end

#inline_hd_chap(chap, id) ⇒ Object



1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
# File 'lib/review/latexbuilder.rb', line 1146

def inline_hd_chap(chap, id)
  n = chap.headline_index.number(id)
  if n.present? && chap.number && over_secnolevel?(n)
    str = I18n.t('hd_quote', [chap.headline_index.number(id), compile_inline(chap.headline(id).caption)])
  else
    str = I18n.t('hd_quote_without_number', compile_inline(chap.headline(id).caption))
  end
  if @book.config['chapterlink']
    anchor = n.gsub(/\./, '-')
    macro('reviewsecref', str, sec_label(anchor))
  else
    str
  end
end

#inline_hi(str) ⇒ Object

hidden index



1059
1060
1061
# File 'lib/review/latexbuilder.rb', line 1059

def inline_hi(str)
  index(str)
end

#inline_hidx(str) ⇒ Object

hidden index



1078
1079
1080
# File 'lib/review/latexbuilder.rb', line 1078

def inline_hidx(str)
  index(str)
end

#inline_i(str) ⇒ Object

index -> italic



1064
1065
1066
1067
1068
1069
1070
# File 'lib/review/latexbuilder.rb', line 1064

def inline_i(str)
  if @book.config.check_version('2', exception: false)
    macro('textit', escape(str))
  else
    macro('reviewit', escape(str))
  end
end

#inline_icon(id) ⇒ Object



1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
# File 'lib/review/latexbuilder.rb', line 1197

def inline_icon(id)
  if @chapter.image(id).path
    command = 'reviewincludegraphics'
    if @book.config.check_version('2', exception: false)
      command = 'includegraphics'
    end
    macro(command, @chapter.image(id).path)
  else
    warn "image not bound: #{id}"
    "\\verb|--[[path = #{id} (#{existence(id)})]]--|"
  end
end

#inline_idx(str) ⇒ Object

index



1073
1074
1075
# File 'lib/review/latexbuilder.rb', line 1073

def inline_idx(str)
  escape(str) + index(str)
end

#inline_img(id) ⇒ Object



995
996
997
998
999
1000
1001
1002
1003
1004
# File 'lib/review/latexbuilder.rb', line 995

def inline_img(id)
  chapter, id = extract_chapter_id(id)
  if get_chap(chapter).nil?
    macro('reviewimageref', I18n.t('format_number_without_chapter', [chapter.image(id).number]), image_label(id, chapter))
  else
    macro('reviewimageref', I18n.t('format_number', [get_chap(chapter), chapter.image(id).number]), image_label(id, chapter))
  end
rescue KeyError
  error "unknown image: #{id}"
end

#inline_list(id) ⇒ Object

FIXME: use TeX native label/ref.



973
974
975
976
977
978
979
980
981
982
# File 'lib/review/latexbuilder.rb', line 973

def inline_list(id)
  chapter, id = extract_chapter_id(id)
  if get_chap(chapter).nil?
    macro('reviewlistref', I18n.t('format_number_without_chapter', [chapter.list(id).number]))
  else
    macro('reviewlistref', I18n.t('format_number', [get_chap(chapter), chapter.list(id).number]))
  end
rescue KeyError
  error "unknown list: #{id}"
end

#inline_m(str) ⇒ Object

math



1050
1051
1052
1053
1054
1055
1056
# File 'lib/review/latexbuilder.rb', line 1050

def inline_m(str)
  if @book.config.check_version('2', exception: false)
    " $#{str}$ "
  else
    "$#{str}$"
  end
end

#inline_pageref(id) ⇒ Object



968
969
970
# File 'lib/review/latexbuilder.rb', line 968

def inline_pageref(id)
  "\\pageref{#{id}}"
end

#inline_raw(str) ⇒ Object



1169
1170
1171
# File 'lib/review/latexbuilder.rb', line 1169

def inline_raw(str)
  super(str)
end

#inline_strong(str) ⇒ Object



1185
1186
1187
# File 'lib/review/latexbuilder.rb', line 1185

def inline_strong(str)
  macro('reviewstrong', escape(str))
end

#inline_sub(str) ⇒ Object



1173
1174
1175
# File 'lib/review/latexbuilder.rb', line 1173

def inline_sub(str)
  macro('textsubscript', escape(str))
end

#inline_sup(str) ⇒ Object



1177
1178
1179
# File 'lib/review/latexbuilder.rb', line 1177

def inline_sup(str)
  macro('textsuperscript', escape(str))
end

#inline_table(id) ⇒ Object



984
985
986
987
988
989
990
991
992
993
# File 'lib/review/latexbuilder.rb', line 984

def inline_table(id)
  chapter, id = extract_chapter_id(id)
  if get_chap(chapter).nil?
    macro('reviewtableref', I18n.t('format_number_without_chapter', [chapter.table(id).number]), table_label(id, chapter))
  else
    macro('reviewtableref', I18n.t('format_number', [get_chap(chapter), chapter.table(id).number]), table_label(id, chapter))
  end
rescue KeyError
  error "unknown table: #{id}"
end

#inline_tcy(str) ⇒ Object



1228
1229
1230
# File 'lib/review/latexbuilder.rb', line 1228

def inline_tcy(str)
  macro('rensuji', escape(str))
end

#inline_title(id) ⇒ Object



956
957
958
959
960
961
962
963
964
965
966
# File 'lib/review/latexbuilder.rb', line 956

def inline_title(id)
  title = super
  if @book.config['chapterlink']
    "\\hyperref[chap:#{id}]{#{title}}"
  else
    title
  end
rescue KeyError
  error "unknown chapter: #{id}"
  nofunc_text("[UnknownChapter:#{id}]")
end

#inline_tt(str) ⇒ Object



1114
1115
1116
1117
1118
1119
1120
# File 'lib/review/latexbuilder.rb', line 1114

def inline_tt(str)
  if @book.config.check_version('2', exception: false)
    macro('texttt', escape(str))
  else
    macro('reviewtt', escape(str))
  end
end

#inline_ttb(str) ⇒ Object



1134
1135
1136
1137
1138
1139
1140
# File 'lib/review/latexbuilder.rb', line 1134

def inline_ttb(str)
  if @book.config.check_version('2', exception: false)
    macro('texttt', macro('textbf', escape(str)))
  else
    macro('reviewttb', escape(str))
  end
end

#inline_tti(str) ⇒ Object



1126
1127
1128
1129
1130
1131
1132
# File 'lib/review/latexbuilder.rb', line 1126

def inline_tti(str)
  if @book.config.check_version('2', exception: false)
    macro('texttt', macro('textit', escape(str)))
  else
    macro('reviewtti', escape(str))
  end
end

#inline_u(str) ⇒ Object



1189
1190
1191
# File 'lib/review/latexbuilder.rb', line 1189

def inline_u(str)
  macro('reviewunderline', escape(str))
end

#inline_uchar(str) ⇒ Object



1210
1211
1212
1213
1214
1215
1216
1217
1218
# File 'lib/review/latexbuilder.rb', line 1210

def inline_uchar(str)
  if @texcompiler && @texcompiler.start_with?('platex')
    # with otf package
    macro('UTF', escape(str))
  else
    # passthrough
    [str.to_i(16)].pack('U')
  end
end

#label(id) ⇒ Object



917
918
919
# File 'lib/review/latexbuilder.rb', line 917

def label(id)
  puts macro('label', id)
end

#latextsize(str) ⇒ Object



1295
1296
1297
# File 'lib/review/latexbuilder.rb', line 1295

def latextsize(str)
  @latex_tsize = str
end

#list(lines, id, caption, lang = nil) ⇒ Object

override Builder#list



377
378
379
380
381
382
383
# File 'lib/review/latexbuilder.rb', line 377

def list(lines, id, caption, lang = nil)
  if highlight_listings?
    common_code_block_lst(id, lines, 'reviewlistlst', 'caption', caption, lang)
  else
    common_code_block(id, lines, 'reviewlist', caption, lang) { |line, idx| code_line('list', line, idx, id, caption, lang) }
  end
end

#listnum(lines, id, caption, lang = nil) ⇒ Object

override Builder#listnum



386
387
388
389
390
391
392
393
# File 'lib/review/latexbuilder.rb', line 386

def listnum(lines, id, caption, lang = nil)
  first_line_num = line_num
  if highlight_listings?
    common_code_block_lst(id, lines, 'reviewlistnumlst', 'caption', caption, lang, first_line_num: first_line_num)
  else
    common_code_block(id, lines, 'reviewlist', caption, lang) { |line, idx| code_line_num('listnum', line, first_line_num, idx, id, caption, lang) }
  end
end

#load_idxdb(file) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/review/latexbuilder.rb', line 67

def load_idxdb(file)
  table = {}
  File.foreach(file) do |line|
    key, value = *line.strip.split(/\t+/, 2)
    table[key] = value
  end
  table
end

#make_code_block_args(title, caption, lang, first_line_num: 1) ⇒ Object



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/review/latexbuilder.rb', line 450

def make_code_block_args(title, caption, lang, first_line_num: 1)
  caption_str = compile_inline((caption || ''))
  if title == 'title' && caption_str == '' && @book.config.check_version('2', exception: false)
    caption_str = '\relax' ## dummy charactor to remove lstname
  end
  lexer = if @book.config['highlight'] && @book.config['highlight']['lang']
            @book.config['highlight']['lang'] # default setting
          else
            ''
          end
  lexer = lang if lang.present?
  args = "language={#{lexer}}"
  if title == 'title' && caption_str == ''
    # ignore
  else
    args = "#{title}={#{caption_str}}," + args
  end
  if first_line_num != 1
    args << ",firstnumber=#{first_line_num}"
  end
  args
end

#nodisp_begin(level, _label, caption) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/review/latexbuilder.rb', line 168

def nodisp_begin(level, _label, caption)
  if @output.pos != 0
    blank
  else
    puts macro('clearpage')
  end
  puts macro('addcontentsline', 'toc', HEADLINE[level], compile_inline(caption))
  # FIXME: headings
end

#nodisp_end(level) ⇒ Object



178
179
# File 'lib/review/latexbuilder.rb', line 178

def nodisp_end(level)
end

#nofunc_text(str) ⇒ Object



1110
1111
1112
# File 'lib/review/latexbuilder.rb', line 1110

def nofunc_text(str)
  escape(str)
end

#noindentObject



929
930
931
# File 'lib/review/latexbuilder.rb', line 929

def noindent
  print '\noindent'
end

#nonum_begin(level, _label, caption) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/review/latexbuilder.rb', line 147

def nonum_begin(level, _label, caption)
  blank unless @output.pos == 0
  @doc_status[:caption] = true
  puts macro(HEADLINE[level] + '*', compile_inline(caption))
  @doc_status[:caption] = nil
  puts macro('addcontentsline', 'toc', HEADLINE[level], compile_inline(caption))
end

#nonum_end(level) ⇒ Object



155
156
# File 'lib/review/latexbuilder.rb', line 155

def nonum_end(level)
end

#notoc_begin(level, _label, caption) ⇒ Object



158
159
160
161
162
163
# File 'lib/review/latexbuilder.rb', line 158

def notoc_begin(level, _label, caption)
  blank unless @output.pos == 0
  @doc_status[:caption] = true
  puts macro(HEADLINE[level] + '*', compile_inline(caption))
  @doc_status[:caption] = nil
end

#notoc_end(level) ⇒ Object



165
166
# File 'lib/review/latexbuilder.rb', line 165

def notoc_end(level)
end

#ol_beginObject



273
274
275
276
277
278
279
# File 'lib/review/latexbuilder.rb', line 273

def ol_begin
  blank
  puts '\begin{enumerate}'
  return true unless @ol_num
  puts "\\setcounter{enumi}{#{@ol_num - 1}}"
  @ol_num = nil
end

#ol_endObject



291
292
293
294
# File 'lib/review/latexbuilder.rb', line 291

def ol_end
  puts '\end{enumerate}'
  blank
end

#ol_item(lines, _num) ⇒ Object



281
282
283
284
285
286
287
288
289
# File 'lib/review/latexbuilder.rb', line 281

def ol_item(lines, _num)
  str = join_lines_to_paragraph(lines)
  unless @book.config['join_lines_by_lang']
    str = lines.map(&:chomp).join("\n")
  end

  str.sub!(/\A(\[)/) { '\lbrack{}' }
  puts '\item ' + str
end

#olnum(num) ⇒ Object



1303
1304
1305
# File 'lib/review/latexbuilder.rb', line 1303

def olnum(num)
  @ol_num = num.to_i
end

#pagebreakObject



921
922
923
# File 'lib/review/latexbuilder.rb', line 921

def pagebreak
  puts '\pagebreak'
end

#paragraph(lines) ⇒ Object



319
320
321
322
323
324
325
326
327
# File 'lib/review/latexbuilder.rb', line 319

def paragraph(lines)
  blank
  if @book.config['join_lines_by_lang']
    puts join_lines_to_paragraph(lines)
  else
    lines.each { |line| puts line }
  end
  blank
end

#parasepObject



329
330
331
# File 'lib/review/latexbuilder.rb', line 329

def parasep
  puts '\\parasep'
end

#parse_metric(type, metric) ⇒ Object



484
485
486
487
488
489
490
# File 'lib/review/latexbuilder.rb', line 484

def parse_metric(type, metric)
  s = super(type, metric)
  if @book.config['pdfmaker']['use_original_image_size'] && s.empty? && !metric.present?
    return ' ' # pass empty to \reviewincludegraphics
  end
  s
end

#quote(lines) ⇒ Object



847
848
849
# File 'lib/review/latexbuilder.rb', line 847

def quote(lines)
  latex_block('quote', lines)
end

#read(lines) ⇒ Object Also known as: lead



333
334
335
# File 'lib/review/latexbuilder.rb', line 333

def read(lines)
  latex_block('quotation', lines)
end

#resultObject



99
100
101
102
103
104
# File 'lib/review/latexbuilder.rb', line 99

def result
  if @chapter.is_a?(ReVIEW::Book::Part) && !@book.config.check_version('2', exception: false)
    puts '\end{reviewpart}'
  end
  @output.string
end

#result_metric(array) ⇒ Object



499
500
501
# File 'lib/review/latexbuilder.rb', line 499

def result_metric(array)
  array.join(',')
end

#separate_tsize(size) ⇒ Object



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'lib/review/latexbuilder.rb', line 720

def separate_tsize(size)
  ret = []
  s = ''
  brace = nil
  size.split('').each do |ch|
    case ch
    when '|'
      next
    when '{'
      brace = true
      s << ch
    when '}'
      brace = nil
      s << ch
      ret << s
      s = ''
    else
      if brace
        s << ch
      else
        if s.empty?
          s << ch
        else
          ret << s
          s = ch
        end
      end
    end
  end

  unless s.empty?
    ret << s
  end

  ret
end

#setup_indexObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/review/latexbuilder.rb', line 45

def setup_index
  @index_db = {}
  @index_mecab = nil
  return true unless @book.config['pdfmaker']['makeindex']

  if @book.config['pdfmaker']['makeindex_dic']
    @index_db = load_idxdb(@book.config['pdfmaker']['makeindex_dic'])
  end
  return true unless @book.config['pdfmaker']['makeindex_mecab']
  begin
    begin
      require 'MeCab'
    rescue LoadError
      require 'mecab'
    end
    require 'nkf'
    @index_mecab = MeCab::Tagger.new(@book.config['pdfmaker']['makeindex_mecab_opts'])
  rescue LoadError
    warn 'not found MeCab'
  end
end

#source(lines, caption = nil, lang = nil) ⇒ Object



473
474
475
476
477
478
479
# File 'lib/review/latexbuilder.rb', line 473

def source(lines, caption = nil, lang = nil)
  if highlight_listings?
    common_code_block_lst(nil, lines, 'reviewsourcelst', 'title', caption, lang)
  else
    common_code_block(nil, lines, 'reviewsource', caption, lang) { |line, idx| code_line('source', line, idx, nil, caption, lang) }
  end
end

#table(lines, id = nil, caption = nil) ⇒ Object



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# File 'lib/review/latexbuilder.rb', line 629

def table(lines, id = nil, caption = nil)
  if caption.present?
    if @book.config.check_version('2', exception: false)
      puts "\\begin{table}[h]%%#{id}"
    else
      puts "\\begin{table}%%#{id}"
    end
  end

  sepidx, rows = parse_table_rows(lines)
  begin
    table_header(id, caption) if caption.present?
  rescue KeyError
    error "no such table: #{id}"
  end
  table_begin(rows.first.size)
  table_rows(sepidx, rows)
  table_end
  if caption.present?
    puts '\end{table}'
  end
  blank
end

#table_begin(ncols) ⇒ Object



699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/review/latexbuilder.rb', line 699

def table_begin(ncols)
  if @latex_tsize
    @tsize = @latex_tsize
  end

  if @tsize
    if @tsize =~ /\A[\d., ]+\Z/
      @cellwidth = @tsize.split(/\s*,\s*/)
      @cellwidth.collect! { |i| "p{#{i}mm}" }
      puts macro('begin', 'reviewtable', '|' + @cellwidth.join('|') + '|')
    else
      @cellwidth = separate_tsize(@tsize)
      puts macro('begin', 'reviewtable', @tsize)
    end
  else
    puts macro('begin', 'reviewtable', (['|'] * (ncols + 1)).join('l'))
    @cellwidth = ['l'] * ncols
  end
  puts '\\hline'
end

#table_endObject



792
793
794
795
796
797
# File 'lib/review/latexbuilder.rb', line 792

def table_end
  puts macro('end', 'reviewtable')
  @tsize = nil
  @latex_tsize = nil
  @cellwidth = nil
end

#table_header(id, caption) ⇒ Object



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/review/latexbuilder.rb', line 682

def table_header(id, caption)
  if id.nil?
    if caption.present?
      @doc_status[:caption] = true
      puts macro('reviewtablecaption*', compile_inline(caption))
      @doc_status[:caption] = nil
    end
  else
    if caption.present?
      @doc_status[:caption] = true
      puts macro('reviewtablecaption', compile_inline(caption))
      @doc_status[:caption] = nil
    end
    puts macro('label', table_label(id))
  end
end

#table_rows(sepidx, rows) ⇒ Object



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/review/latexbuilder.rb', line 653

def table_rows(sepidx, rows)
  if sepidx
    sepidx.times do
      cno = -1
      tr(rows.shift.map do |s|
           cno += 1
           th(s, @cellwidth[cno])
         end)
    end
    rows.each do |cols|
      cno = -1
      tr(cols.map do |s|
           cno += 1
           td(s, @cellwidth[cno])
         end)
    end
  else
    rows.each do |cols|
      h, *cs = *cols
      cno = 0
      tr([th(h, @cellwidth[0])] +
         cs.map do |s|
           cno += 1
           td(s, @cellwidth[cno])
         end)
    end
  end
end

#table_separatorObject



757
758
759
# File 'lib/review/latexbuilder.rb', line 757

def table_separator
  # puts '\hline'
end

#td(s, cellwidth = 'l') ⇒ Object



774
775
776
777
778
779
780
781
782
783
784
785
# File 'lib/review/latexbuilder.rb', line 774

def td(s, cellwidth = 'l')
  if /\\\\/ =~ s
    if !@book.config.check_version('2', exception: false) && cellwidth =~ /\{/
      s.gsub("\\\\\n", '\\newline{}')
    else
      ## use shortstack for @<br>
      macro('shortstack[l]', s)
    end
  else
    s
  end
end

#texequation(lines, id = nil, caption = '') ⇒ Object



861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
# File 'lib/review/latexbuilder.rb', line 861

def texequation(lines, id = nil, caption = '')
  blank

  if id
    puts macro('begin', 'reviewequationblock')
    if get_chap.nil?
      puts macro('reviewequationcaption', "#{I18n.t('equation')}#{I18n.t('format_number_header_without_chapter', [@chapter.equation(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}")
    else
      puts macro('reviewequationcaption', "#{I18n.t('equation')}#{I18n.t('format_number_header', [get_chap, @chapter.equation(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)}")
    end
  end

  puts macro('begin', 'equation*')
  lines.each do |line|
    puts line
  end
  puts macro('end', 'equation*')

  if id
    puts macro('end', 'reviewequationblock')
  end

  blank
end

#th(s, cellwidth = 'l') ⇒ Object



761
762
763
764
765
766
767
768
769
770
771
772
# File 'lib/review/latexbuilder.rb', line 761

def th(s, cellwidth = 'l')
  if /\\\\/ =~ s
    if !@book.config.check_version('2', exception: false) && cellwidth =~ /\{/
      macro('reviewth', s.gsub("\\\\\n", '\\newline{}'))
    else
      ## use shortstack for @<br>
      macro('reviewth', macro('shortstack[l]', s))
    end
  else
    macro('reviewth', s)
  end
end

#tr(rows) ⇒ Object



787
788
789
790
# File 'lib/review/latexbuilder.rb', line 787

def tr(rows)
  print rows.join(' & ')
  puts ' \\\\  \hline'
end

#ul_beginObject



253
254
255
256
# File 'lib/review/latexbuilder.rb', line 253

def ul_begin
  blank
  puts '\begin{itemize}'
end

#ul_endObject



268
269
270
271
# File 'lib/review/latexbuilder.rb', line 268

def ul_end
  puts '\end{itemize}'
  blank
end

#ul_item(lines) ⇒ Object



258
259
260
261
262
263
264
265
266
# File 'lib/review/latexbuilder.rb', line 258

def ul_item(lines)
  str = join_lines_to_paragraph(lines)
  unless @book.config['join_lines_by_lang']
    str = lines.map(&:chomp).join("\n")
  end

  str.sub!(/\A(\[)/) { '\lbrack{}' }
  puts '\item ' + str
end